Buy Tickets
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 16067 | Accepted: 8017 |
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
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.
题意:有N个人排名买票,现在给出每个人要插入的位置pos(0<=pos<=N-1)以及他的价值val。在插入N个人后,会构成一个新序列。现在让你按顺序 输出新序列中每个位置pos对应的价值val。
分析:首先我们知道——最后一个人一定会得到当前队伍他想要的位置,如果我们往前一个阶段,倒数第二个人也一定能得到他想要的位置… So?我们可以选择逆过程插入,这样的话,对于插入在pos位置的人,他的前面一定要有pos个空位(这里0<=pos<=N-1),因此,我们只需要找出前面有pos个空位的节点插入,然后将该节点的空位数清0并维护其它区间的空位数就行了。
思路:用sum[]记录区间剩余空位数。每次插入时,若左儿子的sum[]值大于pos,那么只需要在左儿子找。否则在右儿子找,但是注意pos = pos - 左儿子的sum[](因为左儿子已经有sum[]个空位,我们只需要再找pos - sum[]个就行了)。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 200000+10 using namespace std; struct Node { int pos, val; }; Node num[MAXN]; int sum[MAXN<<2];//记录区间 空位数 void PushUp(int o) { sum[o] = sum[o<<1] + sum[o<<1|1]; } void build(int o, int l, int r) { if(l == r) { sum[o] = 1;//初始每个节点都有一个空位 return ; } int mid = (l + r) >> 1; build(o<<1, l, mid); build(o<<1|1, mid+1, r); PushUp(o); } int rec[MAXN];//记录顺序 int query(int o, int l, int r, int pos) { if(l == r) { sum[o] = 0;//插入 空位数少一 return l; } int mid = (l + r) >> 1; int res; if(sum[o<<1] > pos)//在左儿子找 res = query(o<<1, l, mid, pos); else//在右儿子找 res = query(o<<1|1, mid+1, r, pos - sum[o<<1]); PushUp(o);//维护其它区间的 空位数 return res; } int main() { int N; while(scanf("%d", &N) != EOF) { for(int i = 1; i <= N; i++) scanf("%d%d", &num[i].pos, &num[i].val); build(1, 1, N); for(int i = N; i >= 1; i--) rec[query(1, 1, N, num[i].pos)] = num[i].val; for(int i = 1; i <= N; i++) { if(i > 1) printf(" "); printf("%d", rec[i]); } printf("\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。