POJ 2828-Buy Tickets(线段树单点更新-插队)
2017-04-14 14:41
579 查看
Buy Tickets
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
Sample Output
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
POJ Monthly--2006.05.28, Zhu, Zeyuan
先插入 2 69,则当前 — — 69 —;
再插入 1 33,则当前 — 33 69 —;
再插入 1 51,则当前 — 33 69 51;
后插入 0 77,则当前 77 33 69 51。
规律是根据pos预留下pos个空位,然后插入当前val,此时val位置确定,再确定下一个数的位置时,忽略已经确定的数的位置,以第一个空位置开始再预留pos个空位,,然后插入当前val……直到插入完毕。
用线段树保存左右结点l、r和区间内能放置的人的个数res,用ans记录队列中各个人的val。
倒序插入,根据pos依次向下判断val放左子树还是右子树直到根结点。
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 19874 | Accepted: 9822 |
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
4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31492
Sample Output
77 33 69 51 31492 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
POJ Monthly--2006.05.28, Zhu, Zeyuan
题目意思:
有N个人,给出他们插队时前面的人的个数pos和自己的编号val,依次开始插队,求插队完毕后他们的顺序。解题思路:
倒序插入,这样每次插入之后的位置都是确定的。例如:0 77 1 51 1 33 2 69
先插入 2 69,则当前 — — 69 —;
再插入 1 33,则当前 — 33 69 —;
再插入 1 51,则当前 — 33 69 51;
后插入 0 77,则当前 77 33 69 51。
规律是根据pos预留下pos个空位,然后插入当前val,此时val位置确定,再确定下一个数的位置时,忽略已经确定的数的位置,以第一个空位置开始再预留pos个空位,,然后插入当前val……直到插入完毕。
用线段树保存左右结点l、r和区间内能放置的人的个数res,用ans记录队列中各个人的val。
倒序插入,根据pos依次向下判断val放左子树还是右子树直到根结点。
#include <iostream> #include <cstdio> using namespace std; #define INF 0xfffffff #define MAXN 200000 int ans[MAXN]; struct Node //树 { int l,r;//左右节点 int res;//区间内的剩余位置 }; Node tree[MAXN<<2]; struct node { int pos; int val; } a[MAXN]; void BuildTree(int root, int l, int r)//建树 { tree[root].l=l; tree[root].r=r; tree[root].res=1;//该位置能放一人 if(l!=r) { BuildTree(2*root+1,l,(l+r)/2); BuildTree(2*root+2,(l+r)/2+1,r); tree[root].res=tree[2*root+1].res+tree[2*root+2].res;//该区间能放的人数 } } void Insert(int i,int pos,int val)//插入 { if(tree[i].l==tree[i].r)//成功插入队列 { ans[tree[i].l]=val;//记录 tree[i].res=0;//当前已经插入所以不能再放 return ; } if(pos<tree[2*i+1].res)//插入的位置若能放入左子树 Insert(2*i+1,pos,val); else//走右子树 Insert(2*i+2,pos-tree[2*i+1].res,val); tree[i].res--;//放完后该节点的人数减一 } int main() { #ifdef ONLINE_JUDGE #else freopen("G:/cbx/read.txt","r",stdin); //freopen("G:/cbx/out.txt","w",stdout); #endif int n; while(scanf("%d",&n)!=EOF) { BuildTree(0,1,n); for(int i=1; i<=n; ++i) scanf("%d%d",&a[i].pos,&a[i].val); for(int i=n; i>=1; --i)//倒序插入 Insert(0,a[i].pos,a[i].val); for(int i=1; i<n; ++i) printf("%d ",ans[i]); printf("%d\n",ans ); } return 0; }
相关文章推荐
- POJ 2828 Buy Tickets (线段树 单点更新 插队问题)
- poj 2828 Buy Tickets(线段树单点更新)
- Buy Tickets----POJ_2828----线段树之单点更新
- poj 2828 Buy Tickets (线段树---单点更新)
- POJ - 2828 Buy Tickets (线段树单点更新)
- 【poj】2828 Buy Tickets、2182 Lost Cow(线段树-单点更新)
- poj 2828 Buy Tickets(线段树-单点更新)
- POJ 2828 Buy Tickets (线段树 单点更新 变形)
- poj 2828 Buy Tickets 【线段树】【逆序插入 + 单点更新 + 区间求和】
- POJ 2828 Buy Tickets ( 线段树—单点更新 )
- poj-----(2828)Buy Tickets(线段树单点更新)
- POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】
- poj 2828 Buy Tickets(动态队列·线段树单点更新)
- poj 2828 插队运用(线段树,单点更新)
- POJ 2828 (线段树 单点更新) Buy Tickets
- poj2828 Buy Tickets 线段树,单点更新
- POJ 题目2828 Buy Tickets(线段树单点更新)
- POJ 2828 Buy Tickets(线段树--单点更新)
- POJ 2828 Buy Tickets(线段树单点更新维护)
- POJ 2828 Buy Tickets 线段树 单点更新