POJ - Buy Tickets

发布时间 2023-08-01 15:32:19作者: smiling&weeping

Smiling & Weeping

                    ----你看这个人,嘴里说着喜欢我

                      却又让我这么难过

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
思路:我们要学会逆序化的思维方式,我们插入数据(看似是一道普通的链表题目,但是你会发现链表的查询个数很耗时间)使用链表会在查询插队位置上
消耗大量的时间TLE,所以我们要学会使用逆序化的思维方式,最后插入的元素是直接排在第x人的后面(最后插入的人位置永远是固定的),那我们就可以
倒序找前面是否有x个空位置(没人设置为1,有人设置为0)
题目链接:2828 -- Buy Tickets (poj.org)
Talk is cheap , Show me your code
 1 //我们要学会逆向的思维方式,最后出现的一定是位置确定的
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<vector>
 8 using namespace std;
 9 #define ls(p) p<<1
10 #define rs(p) p<<1|1
11 const int maxn = 200005;
12 int tree[maxn<<2] , num[maxn] , n;
13 void push_up(int p)
14 {
15     tree[p] = tree[ls(p)] + tree[rs(p)];
16 }
17 void build(int p, int pl , int pr)
18 {
19     if(pl == pr)
20     {
21         tree[p] = 1;
22         num[pl] = 0;
23         return ;
24     }
25     int mid = pl+pr >> 1;
26     build(ls(p) , pl , mid);
27     build(rs(p) , mid+1 , pr);
28     push_up(p);
29 }
30 void update(int L ,int R , int p, int pl , int pr , int ind , int v)
31 {
32     if(pl == pr)
33     {
34         tree[p] = 0;
35         num[pl] = v;
36         return ;
37     }
38     int mid = pl+pr >> 1;
39     if(ind <= tree[ls(p)]) update(L , R , ls(p) , pl , mid , ind , v);
40     else
41         update(L , R , rs(p) , mid+1 , pr , ind-tree[ls(p)] , v);
42     push_up(p);
43 }
44 int main()
45 {
46     while(scanf("%d",&n) != EOF)
47     {
48         int a , b;
49         build(1,1,n);
50         vector<pair<int , int> > p;
51         for(int i = 1; i <= n; i++)
52         {
53             scanf("%d%d",&a,&b);
54             p.push_back(make_pair(a+1,b));
55         }
56         for(int i = n-1; i >= 0; i--)
57             update(1 , n , 1 ,1 , n , p[i].first , p[i].second);
58         for(int i = 1; i <= n; i++)
59             printf("%d ",num[i]);
60         printf("\n");
61     }
62     return 0;
63 }

山海自有归期,风雨自有相逢

难过(ಥ﹏ಥ)