其他分享
首页 > 其他分享> > poj 2828 buy Tickets 用线段树模拟带插入的队列

poj 2828 buy Tickets 用线段树模拟带插入的队列

作者:互联网

Buy Tickets

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2795

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 ≤ iN). For each i, the ranges and meanings of Posi and Vali are as follows:

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

 

题意

给你一个队列,有一群人来插队,然后让你输出最后队列的样子

题解:

看完题解感觉此题很水,但是我太弱了,并没有想出来。

假如第i个人要插到第k个位置,那么如果后面有人插队他就会往后移,所以我们自然的想从后往前插入。

这样第i个人就是插到第k+1个空位上(即前面的的有k个位置暂时没有人,并且这个位置也没有人)。

用线段维护一下就好了,单点修改,单点查询。

代码:

 

 1     //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 400050
 7 int n,ans[N];
 8 struct Qurey{int x,val;}que[N];
 9 struct Tree{int l,r,sum;}tr[N<<2];
10 template<typename T>void read(T&x)
11 {
12   int k=0; char c=getchar();
13   x=0;
14   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
15   if(c==EOF)exit(0);
16   while(isdigit(c))x=x*10+c-'0',c=getchar();
17   x=k?-x:x;
18 }
19 void bt(int x,int l,int r)
20   {
21     tr[x]=Tree{l,r,r-l+1};
22     if (l==r)return;
23     int mid=(l+r)>>1;
24     bt(x<<1,l,mid);
25     bt(x<<1|1,mid+1,r);
26   }
27 int update(int x,int tt)
28 {
29   if(tr[x].l==tr[x].r)
30     {
31       tr[x].sum--;
32       return tr[x].l;
33     }
34   int ans=0;
35   if (tr[x<<1].sum>=tt) ans=update(x<<1,tt);
36   else ans=update(x<<1|1,tt-tr[x<<1].sum);
37   tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum;
38   return ans;
39 }
40 void work()
41 {
42   read(n);
43   for(int i=1;i<=n;i++)
44     {
45       read(que[i].x);
46       read(que[i].val);
47     }
48   bt(1,1,n);
49   for(int i=n;i>=1;i--)
50     {
51       int k=update(1,que[i].x+1);
52       ans[k]=que[i].val;
53     }
54   for(int i=1;i<=n;i++)printf("%d ",ans[i]);
55   putchar('\n');
56 }
57 int main()
58 {
59 #ifndef ONLINE_JUDGE
60   freopen("aa.in","r",stdin);
61 #endif
62   while(1)
63     {
64       work();
65     }
66 }
View Code

 

标签:Tickets,buy,people,int,queue,person,2828,test,was
来源: https://www.cnblogs.com/mmmqqdd/p/10736775.html