POJ-2828 Buy Tickets
来源:互联网 发布:阿里云备份 编辑:程序博客网 时间:2024/06/07 21:03
Description
Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…
The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.
It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!
People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.
Input
There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:
- Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
- Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.
There no blank lines between test cases. Proceed to the end of input.
Output
For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.
Sample Input
40 771 511 332 6940 205231 192431 38900 31492
Sample Output
77 33 69 5131492 20523 3890 19243
Hint
The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.
Source
————————————————————思考的分割线————————————————————
题意:考虑到这题还算难读,说一下,就是一个一个插队。Pos是插队时距前台的距离。问最后的顺序。
来源:http://www.cnblogs.com/Missa/archive/2012/07/30/2615907.html
思路:神犇胡浩大大给出了单点更新的题目后,给出两道练习题。奈何我等弱菜不会做,找题解,思路瞬间被打开,然后还是不会做……于是就有了上面的链接。
思维方式是倒过来想。对于插队这种现象,谁最有优势?换句话说,对于最后的顺序,谁说了算?(现在只考虑静态的)当然,是最后一个插队的,他插完队之后,队就定型了。倒着看Input,最后一个人给出了一个很有用的数据:他前面有几个人。这就意味着先把他放进线段树中那个位置是肯定不可能出错的。然后考虑倒着插队时,每个人给出的Pos[i]都是一个值:他前面有几个人。但是倒着看的时候,你会发现只要不是最后一人,给出的Pos[i]都是动态的,只针对他插队的时候(顺序又是正的了)。没关系,抹掉他“之前”插队的人就行了。(我们是倒着看的!)
倒着插队的话,每个人完成插队之后位置就立刻确定。我们“抹掉”这个位置,然后看“下一个”,“下一个”人给出的Pos如果是0的话,就是当初他直接插到了队首。不是0的话,意味着他插队的时候,比较“礼让”,没有插到队首!那意味着“下一个”人给出了一个准确的数值:他前面有几个空位他没去插。留着,给“后来的”人插。
这样一来,线段树就出来了。树上保存“没插的位置数”,然后从最后一个人开始一个一个插队。其实对最后一个人的Pos[i]也是同样的,他前面有Pos个位置他没去插。首先让他插,插完之后抹掉他的位置,考虑“下一个”人,他给出的Pos[i]意义就相当清晰了。
代码如下:
/****************************************/ #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #include <queue> #include <vector> #include <map> #include <string> #include <iostream> using namespace std;/****************************************/#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1const int N = 200010;int tree[N<<2], ans[N], pos[N], val[N];void build(int l, int r, int rt) {tree[rt] = r - l + 1;//树上各结点保存留着的空位数if(l == r)return ;int m = (l+r) >> 1;build(lson);build(rson);}int update(int man, int l, int r, int rt) {tree[rt]--;//自上而下更新空位数,每更新一个人,该区间空位数肯定减少1if(l == r)return l;//返回他的准确位置int m = (l+r) >> 1;if(man < tree[rt<<1])return update(man, lson);//倘若左子树空位数大于man,这个人一定在左子树内else {//左子树空位数和他给出的数相等或者更小,他一定到了右子树中man -= tree[rt<<1];//剪掉左子树的人数,看他在右子树的位置return update(man, rson);}}int main() {int n;while(~scanf("%d", &n)) {build(1, n, 1);for(int i = 1; i <= n; i++)scanf("%d%d", pos+i, val+i);for(int i = n; i > 0; i--) {int ret = update(pos[i], 1, n, 1);//倒着来看的话,每个人插队的时候,他前面一定有pos[i]个位置是留着没插的。ans[ret] = val[i];}for(int i = 1; i < n; i++)printf("%d ", ans[i]);printf("%d\n", ans[n]);}return 0;}
顺便提一句:G++——3000+ms过的。这题用树状数组(BIT)好像可以1000ms过……再提一句:这份线段树的代码用C++交,就是1000+ms……
- poj 2828 Buy Tickets
- poj 2828 Buy Tickets
- poj 2828 Buy Tickets
- poj 2828 Buy Tickets
- POJ 2828 Buy Tickets
- Poj 2828 Buy Tickets
- poj 2828 buy tickets
- POJ 2828 Buy Tickets
- poj 2828 Buy Tickets
- poj 2828 Buy Tickets
- POJ 2828 - Buy Tickets
- POJ 2828 - Buy Tickets
- POJ-2828-Buy Tickets
- poj 2828 Buy Tickets
- poj 2828 Buy Tickets
- poj 2828 Buy Tickets
- POJ 2828 Buy Tickets
- POJ 2828 Buy Tickets
- 重新编译Nginx指导手册【修复静态编译Openssl的Nginx漏洞 】[Openssl Heartbleed]
- 谁从汽车电子标准中获益
- HDU 3666 THE MATRIX PROBLEM 差分约束
- Fragment使用总结
- mac os x10.9下如何使用git与github
- POJ-2828 Buy Tickets
- Python3.x学习笔记[2.5]灵活使用urllib与re
- java 死锁及解决
- 编程:关于testing和debugging
- Noi2012四川省选day1 喵星球上的点名_(后缀数组) 代码及解析(详细)
- ZOJ 3430 Detect the Virus(AC自动机+字符串转换)
- error LNK2019: unresolved external symbol "public: class CVD::ImageRef __thiscall VideoSou
- QTP 描述性编程 VS 录制
- 交通灯模拟系统