Buy Tickets
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 15422 | Accepted: 7684 |
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 Valiin 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个人来排队,第一个数是这个人要插在哪个位子(插入后,其他人往后退一位),最终输出人的位子的顺序。
思路:
正着插入的时候其他人的标号都往后移,数据范围太大,时间复杂度承受不了。
那么反着插入呢?
因为题目要求数据的正确性,假设插入 2 1000 的时候,那么保证他之前一定有两个人。所以一种思路呼之欲出了,反向插入人,当插入他的时候就保留前面几个空位。线段树维护区间空位数即得出答案。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 200005 12 #define ll root<<1 13 #define rr root<<1|1 14 #define mid (a[root].l+a[root].r)/2 15 16 int n; 17 18 struct mm{ 19 int id, num; 20 }b[N]; 21 22 struct node{ 23 int l, r, sum, num; 24 }a[N*4]; 25 26 void build(int l,int r,int root){ 27 a[root].l=l; 28 a[root].r=r; 29 a[root].sum=r-l+1; 30 a[root].num=0; 31 if(l==r) return ; 32 build(l,mid,ll); 33 build(mid+1,r,rr); 34 } 35 36 void update(int sum,int num,int root){ 37 if(a[root].l==a[root].r){ 38 a[root].num=num; 39 a[root].sum--; 40 return; 41 } 42 if(a[ll].sum>=sum) update(sum,num,ll); 43 else update(sum-a[ll].sum,num,rr); 44 a[root].sum=a[ll].sum+a[rr].sum; 45 } 46 47 int ans[N]; 48 49 void out(int root){ 50 if(a[root].l==a[root].r){ 51 ans[a[root].l]=a[root].num;return; 52 } 53 out(ll); 54 out(rr); 55 } 56 57 main() 58 { 59 int i, j, k; 60 while(scanf("%d",&n)==1){ 61 for(i=0;i<n;i++) scanf("%d %d",&b[i].id,&b[i].num); 62 build(1,n,1); 63 for(i=n-1;i>=0;i--){ 64 update(b[i].id+1,b[i].num,1); 65 } 66 // printf("11111\n"); 67 out(1); 68 printf("%d",ans[1]); 69 for(i=2;i<=n;i++) printf(" %d",ans[i]); 70 printf("\n"); 71 } 72 }