poj-2828 Buy Tickets(线段树,排队问题,逆向思维)

题目地址:POJ 2828 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

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.

题意: 
排队买票时候插队。 
给出一些数对,分别代表某个人的想要插入的位置pos_i和他的val_i,求出最后的队列的val顺序。

分析: 
这是一道很巧妙的题目。 

观察发现,最后一个插入到该位置的人位置是固定的,那么我们可以从后面进行插入操作,pos ,val 代表val要插入到pos位置,那么就是说 pos 前面要留出 pos个位置,

我们可以反着来考虑,从后面开始站,后面的人位置一旦确定就不会再改变了,假设后面的人都已经站在正确的位置上了,那么到那个人站的时候,现在的位置上已经都是后面的那些人了,只要数num个空格,那那个人站的位置能确定了。

举个例子,例子来于crazy_apple,谢~

线段树节点中保存这一段中的空位数,然后倒序对pos插入:

    例如:  0 77
         1 51
         1 33
         2 69

  先取: 2 69  ——  ——  —69—   ——   (需要前面有3个空位才能插入)

       然后取: 1 33   ——   —33—    —69—    ——   (需要前面有2个空位才能插入)

       然后取: 1 51   ——   —33—    —69—    —51—   (需要前面有2个空位才能插入)  前面只有1个空位  故插入后面空格

  然后取: 0 77   —77—   —33—    —69—    —51—   (需要前面有1个空位才能插入)

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 #include <math.h>
10 const int INF=0x3f3f3f3f;
11 typedef long long LL;
12 const int maxn=2e5+10;
13 using namespace std;
14 
15 int n;
16 int Add[maxn<<2];//空位数
17 int pos[maxn];//要插入的位置 
18 int val[maxn];//初始的值 
19 int ans[maxn];//最终站位 
20 
21 void Build(int l,int r,int rt)
22 {
23     Add[rt]=r-l+1;//初始空位数 
24     if(l==r)
25         return ;
26     int m=(l+r)>>1;
27     Build(l,m,rt<<1);
28     Build(m+1,r,rt<<1|1); 
29 }
30 
31 int Update(int l,int r,int num,int rt)//num为插队需要前面有多少个空位 
32 {
33     Add[rt]--;//空位数减1
34     if(l==r)
35         return l;//返回插入位置
36     int m=(l+r)>>1;
37     if(Add[rt<<1]>=num)//当左孩子的空格大于等于插入位置时往左边插入
38         Update(l,m,num,rt<<1);
39     else//当左边的空格小于num,则插入右边,插入右边位置num应该减左边的空格数
40     {
41         num-=Add[rt<<1];
42         Update(m+1,r,num,rt<<1|1);
43     }
44 }
45 
46 int main()
47 {
48     while(~scanf("%d",&n))
49     {
50         Build(1,n,1);
51         for(int i=1;i<=n;i++)
52         {
53             scanf("%d %d",&pos[i],&val[i]);
54         }
55         for(int i=n;i>=1;i--)//倒过来更新,则可以确定最后的位置
56         {
57             int key=Update(1,n,pos[i]+1,1);//得到插入地方
58             ans[key]=val[i];//存入ans数组
59         }
60         for(int i=1;i<=n;i++)
61         {
62             if(i!=1)
63                 printf(" ");
64             printf("%d",ans[i]);
65         }
66         printf("\n");
67     }
68     return 0;
69 }

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11281076.html