Codeforces Round #279 (Div. 2) B. Queue 模拟
2015-07-22 13:31
453 查看
思路:
和
中出现了0的行,代表的是队首和队尾元素,所以只需沿着两个0前向/逆向出发,即可将队列填满。
由队尾给出的那一行一定为:ID[n - 1], 0,所以从0逆向出发能确定第n - 1个人,根据n的奇偶性进行判断:
①当n为奇数时,从0前向出发,依次可以确定队列的第2,4,……,n - 1个人,所以应该从队尾元素逆向出发,确定队列的第n,n - 2,……,1个人
②当n为偶数时,从0前向出发,依次可以确定队列的第2,4,……,n个人,从0逆向出发确定队列的第n - 1,n -3,……,1个人
代码如下:
和
中出现了0的行,代表的是队首和队尾元素,所以只需沿着两个0前向/逆向出发,即可将队列填满。
由队尾给出的那一行一定为:ID[n - 1], 0,所以从0逆向出发能确定第n - 1个人,根据n的奇偶性进行判断:
①当n为奇数时,从0前向出发,依次可以确定队列的第2,4,……,n - 1个人,所以应该从队尾元素逆向出发,确定队列的第n,n - 2,……,1个人
②当n为偶数时,从0前向出发,依次可以确定队列的第2,4,……,n个人,从0逆向出发确定队列的第n - 1,n -3,……,1个人
代码如下:
#include <cstdio> using namespace std; #define N 1000005 int pre , next , ans[200005], back[200005]; bool front ; int main(){ int n, x, y, tail; scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%d %d", &x, &y); next[x] = y, pre[y] = x; front[x] = true; back[i] = y; } for(int i = 0; i < n; ++i){ if(!front[back[i]]){ tail = back[i]; break; } } int idx = 2; for(int i = next[0]; i != 0; i = next[i]) ans[idx] = i, idx += 2; int last = pre[0]; idx = n - 1; if(n % 2) ans = tail, last = pre[tail], idx = n - 2; for(int i = last; i != 0; i = pre[i]) ans[idx] = i, idx -= 2; for(int i = 1; i <= n; ++i) printf("%d ", ans[i]); printf("\n"); return 0; }
相关文章推荐
- android <viewStub />, <requestFocus />, <merge /> and <include />用法
- hdu 5288 OO’s Sequence 枚举因数
- 【UIKit】-10-UIAlertView - iOS 8 之后弃用,改用 UIAlertViewController
- iOS编程第四版第12,13章---Touch Events and UIResponder, UIGestureRecognizer and UIMenuController
- UILocalNotification 的使用
- 【UIKit】-9-UIAlertController - IOS8 包括 alert 和 sheet
- 【UIkit】-8-UIActionSheet - 底部弹出提示,快要被取代
- 思维题 HDOJ 5288 OO’s Sequence
- [tableView reloadData]方法添加动画的方法
- C#使用队列(Queue)解决简单的并发问题
- 使用Arduino与A4988 驱动步进电机
- Android异步处理三:Handler+Looper+MessageQueue深入详解
- Android异步处理二:使用AsyncTask异步更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- easy-ui
- zoj 1649 Rescue bfs
- 【UIKit】-5,6,7-UIActivity - 系统服务,社交信息等
- 几个交互设计的原则
- The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....
- Android学习笔记之——Notification.Builder类的用法