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

 
 
题目意思:
给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 }

 

转载于:https://www.cnblogs.com/qq1012662902/p/4527912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值