POJ 2828 单点更新(好题)

Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 15086   Accepted: 7530

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 Valiin 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

POJ Monthly--2006.05.28, Zhu, Zeyuan

题目意思:

给n个人信息,posi表示插到第i个人后面,vali表示这个人的价值,输出插入完毕后价值序列。

思路:

从前往后的话很难写出来,因为后面的人会影响前面人的位置。那么就从后往前,直接用例子解释吧

5

0 1

0 2

1 3

1 4

0 5

从后往前,先插入第5个人

插入的时候这个人前面肯定为0个人,那么序列为5 -1 -1 -1 -1(-1表示这个位置为空)

插入第4个人

插入的时候这个人前面肯定有1个人,那么给那个人留一个空位,序列为5 -1 4 -1 -1

插入第3个人

前面肯定有1个人,给那个人留下一个空位,序列为5 -1 4 3 -1

插入第2个人

前面肯定有0个人,不留空位,序列为5 2 4 3 -1

插入第1个人

只能放在最后面了   序列为5 2 4 3 1

这样的话,套个线段树就可以了。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 #include <set>
 9 using namespace std;
10
11 #define N 2000005
12 #define ll root<<1
13 #define rr root<<1|1
14 #define mid (a[root].l+a[root].r)/2
15
16
17 int max(int x,int y){return x>y?x:y;}
18 int min(int x,int y){return x<y?x:y;}
19 int abs(int x,int y){return x<0?-x:x;}
20
21 int n;
22 int pos[N], val[N];
23 int ans[N];
24
25 struct node{
26     int l, r, num;
27 }a[N];
28
29 void build(int l,int r,int root){
30     a[root].l=l;
31     a[root].r=r;
32     if(l==r) {
33         a[root].num=1;
34         return;
35     }
36     build(l,mid,ll);
37     build(mid+1,r,rr);
38     a[root].num=a[ll].num+a[rr].num;
39 }
40
41 void solve(int id,int root){
42     if(a[root].l==a[root].r){
43         ans[a[root].l]=val[id];
44         a[root].num--;
45         return;
46     }
47     if(a[ll].num>=pos[id]+1) solve(id,ll);//前面有pos[id]+1个空位,其中一个是自己的
48     else {//否则往后面(右子树)插入
49         pos[id]-=a[ll].num;
50         solve(id,rr);
51     }
52     a[root].num=a[ll].num+a[rr].num;
53 }
54
55 main()
56 {
57     int i, j, k;
58     while(scanf("%d",&n)==1){
59         for(i=1;i<=n;i++){
60             scanf("%d %d",&pos[i],&val[i]);
61         //    pos[i]++;
62         }
63         build(1,n,1);
64         for(i=n;i>=1;i--){
65             solve(i,1);
66         }
67         printf("%d",ans[1]);
68         for(i=2;i<=n;i++) printf(" %d",ans[i]);
69         cout<<endl;
70     }
71 }
时间: 2024-10-18 22:14:46

POJ 2828 单点更新(好题)的相关文章

poj 1195 单点更新 区间求和

Mobile phones Time Limit: 5000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate a

poj 2828(单点修改)

题意:一家店门口有n个人在排队,按顺序给出每个人要站在第几个人的后面,假设店是第0个人,给每个人一个代号,问最后代号的顺序是什么. 题解:因为后面的人有可能占前面的人的位置,所以倒着来,每个人位置的含义变成了给前面留多少个空位,线段树实现. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 200005; int n, s[N <

POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Description BackgroundRaymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just b

POJ 2828 Buy Tickets (线段树 单点更新 变形)

题目链接 题意:有N个人排队,给出各个人想插队的位置和标识,要求输出最后的序列. 分析:因为之前的序列会因为插队而变化,如果直接算时间复杂度很高,所以可以用 线段树逆序插入,把序列都插到最后一层,len记录该区间里还剩余多少位置,插入的时候就插到剩余的第几个位置, 比如1,2已经插入了,如果再想插入第3个位置,则实际上插入的是5. 因为是逆序的,所以在3之前除了现在的1,2 还有之前的1,2. 1 #include <iostream> 2 #include <cstdio> 3

线段树(单点更新) POJ 2828 Buy tickets

题目传送门 1 /* 2 结点存储下面有几个空位 3 每次从根结点往下找找到该插入的位置, 4 同时更新每个节点的值 5 */ 6 #include <cstdio> 7 #define lson l, m, rt << 1 8 #define rson m+1, r, rt << 1 | 1 9 10 const int MAX_N = 200000 + 10; 11 int pos[MAX_N], val[MAX_N]; 12 int sum[MAX_N <&

POJ 2828 Buy Tickets(神题!线段树or树状数组)

题目链接:POJ 2828 Buy Tickets [题意]给了你 n(1<=n<=200000)个人的插队信息,让你输出最终的队列的排列 [思路]常规思考的话,这道题就是模拟,但是时间复杂度一定会很高.POJ的discuss上说这道题是神题,难得不是用什么数据结构,而是思路,这道题要逆向去思考,从最后一个人往前看,后插进来的人更容易定位,他一定能站到他想的位置,并且不会在挪动.再前一个人呢?他的位置即是接下来的空位的编号.于是有线段树用于维护位置信息.当然用树状数组也是可以做的,但是要加上二

POJ训练计划2828_Buy Tickets(线段树/单点更新)

解题报告 题意: 插队完的顺序. 思路: 倒着处理数据,第i个人占据第j=pos[i]+1个的空位. 线段树维护区间空位信息. #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node { int x,v; } num[201000]; int sum[1000000],ans[201000]; void cbtree(int rt,int l,in

HDU 1166 敌兵布阵(线段树单点更新,板子题)

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 87684    Accepted Submission(s): 36912 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务

poj 2763 树链剖分(单点更新,区间求值)

http://poj.org/problem?id=2763 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional ro