POJ2828:Buy Tickets

来源:互联网 发布:百叶窗算法 编辑:程序博客网 时间:2024/06/05 11:43
点击打开题目链接

Buy Tickets

Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 10042 Accepted: 4842

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

POJ Monthly--2006.05.28, Zhu, Zeyuan


=====================================题目大意=====================================


按照顺序给出N个插队的人插入队伍的位置以及标志该人的权值,输入最后队伍的序列(即输出最后的权值序列)。

=====================================算法分析=====================================


因为:当前插队的人可能改变之前所有人的位置但却不会改变之后任何人的位置。

所以:本题应当逆序考虑。

这样:在逆序考虑过程中,一旦确定某个人的位置,那这个位置一定为其最终位置。

假设:当前考虑的是第Cur个插队的人,其插入队伍的位置为Pos[Cur],标志该人的权值为Val[Cur](以下用Val[X]表示第X个插队的

      人)。

显然:Val[Cur]对于在其之前插队的所有人Val[Befor](Befor<Cur)而言相对位置为Pos[Cur]。

而且:每个Val[Befor]的最终位置Pos[Befor]当前都是空位(指没有确定最终人选):因为这是逆序考虑。

所以:Val[Cur]在队伍中的最终位置便是第Pos[Cur]+1个空位的位置。

那么:可以通过建立线段树记录区间空位来解决本题。


=======================================代码=======================================




#include<stdio.h>#define Lson(n)  (n<<1)#define Rson(n)  (n<<1|1)#define MID(a,b) (a+b>>1)const int MAXN=200005;int N,P[MAXN],V[MAXN],Loc,Ans[MAXN],SegmTree[MAXN<<2];void Build(int L,int R,int N){    SegmTree[N]=R-L+1;    if(L!=R)    {        int M=MID(L,R);        Build(L,M,Lson(N));        Build(M+1,R,Rson(N));    }}void Insert(int Pos,int L,int R,int N){    --SegmTree[N];    if(L==R)    {        Loc=L;    }    else    {        int M=MID(L,R);        if(SegmTree[Lson(N)]>Pos) Insert(Pos,L,M,Lson(N));        else Insert(Pos-SegmTree[Lson(N)],M+1,R,Rson(N));    }}int main(){    while(scanf("%d",&N)==1)    {        Build(0,N-1,1);        for(int i=0;i<N;++i)        {            scanf("%d%d",&P[i],&V[i]);        }        for(int i=N-1;i>=0;--i)        {            Insert(P[i],0,N-1,1);            Ans[Loc]=V[i];        }        for(int i=0;i<N;++i)        {            printf("%d%c",Ans[i],i+1<N?' ':'\n');        }    }}