题意
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 25813 | Accepted: 12368 |
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.
Source
有n个人排队买票,但是可以插队。售票员在第0号位置,买票的人依次排在右边。现在给你n个人的数据:pi,vi.其中pi表示第i个人排队时站在第pi个人后面,他的val值为vi。现在从小到达给出所有的pi,vi.问最后排队的人的vi值的顺序。
分析
参照STILLxjy的题解。
其实题目的意思很简单,就是将第i个人插在第pi个人后面,问最后的顺序。
若我们模拟这个过程,不管是数组还是链表都是O(n^2)的时间复杂度,会超时。
正向思维无法解决,那我们就逆向思维。
现在我们知道总共有 n 个人,且最后一个人要排在这 n 个人中的位置是 p[n],我们可以直接把队列的第 p[n] 个位子安排给第 n 个人
现在来考虑第 n-1 个人,他要排在只有 n-1 个人的第 p[n-1] 个位置上,怎么做呢?我们肯定是从队列的第 1 个开始数,遇到没有被人占领的位置就加 1,直到数到了 p[n-1],那么这个空位就安排给第 n-1 个人
对于第 n-2 个人,以及剩下的所有人都可以这样做
….
所以现在问题转化成了如何快速求出前p[n]个空位的位置,用树状数组即可。时间复杂度(O(n log n))
代码
#include<iostream>
#include<cmath>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
co int N=2e5+1;
int n,c[N],p[N],v[N],ans[N];
void add(int p,int v){
for(int i=p;i<=n;i+=i&-i) c[i]+=v;
}
int ask(int p){
int i=0,s=0;
for(int k=log((double)n)/log(2.0);k>=0;--k)
if(i+(1<<k)<=n&&s+c[i+(1<<k)]<p)
i+=1<<k,s+=c[i]; // edit 1:c[i]
return i+1;
}
void Buy_Tickets(){
// memset(c,0,sizeof c);
for(int i=1;i<=n;++i) add(i,1);
for(int i=1;i<=n;++i) read(p[i]),read(v[i]);
for(int i=n,t;i;--i) t=ask(p[i]+1),ans[t]=v[i],add(t,-1);
for(int i=1;i<=n;++i) printf("%d ",ans[i]);
puts("");
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
while(~scanf("%d",&n)) Buy_Tickets();
return 0;
}