POJ 2828 线段树(想法)

Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 15422   Accepted: 7684

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个人来排队,第一个数是这个人要插在哪个位子(插入后,其他人往后退一位),最终输出人的位子的顺序。

思路:

正着插入的时候其他人的标号都往后移,数据范围太大,时间复杂度承受不了。

那么反着插入呢?
因为题目要求数据的正确性,假设插入 2  1000 的时候,那么保证他之前一定有两个人。所以一种思路呼之欲出了,反向插入人,当插入他的时候就保留前面几个空位。线段树维护区间空位数即得出答案。

代码:

 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 200005
12 #define ll root<<1
13 #define rr root<<1|1
14 #define mid (a[root].l+a[root].r)/2
15
16 int n;
17
18 struct mm{
19     int id, num;
20 }b[N];
21
22 struct node{
23     int l, r, sum, num;
24 }a[N*4];
25
26 void build(int l,int r,int root){
27     a[root].l=l;
28     a[root].r=r;
29     a[root].sum=r-l+1;
30     a[root].num=0;
31     if(l==r) return ;
32     build(l,mid,ll);
33     build(mid+1,r,rr);
34 }
35
36 void update(int sum,int num,int root){
37     if(a[root].l==a[root].r){
38         a[root].num=num;
39         a[root].sum--;
40         return;
41     }
42     if(a[ll].sum>=sum) update(sum,num,ll);
43     else update(sum-a[ll].sum,num,rr);
44     a[root].sum=a[ll].sum+a[rr].sum;
45 }
46
47 int ans[N];
48
49 void out(int root){
50     if(a[root].l==a[root].r){
51         ans[a[root].l]=a[root].num;return;
52     }
53     out(ll);
54     out(rr);
55 }
56
57 main()
58 {
59     int i, j, k;
60     while(scanf("%d",&n)==1){
61         for(i=0;i<n;i++) scanf("%d %d",&b[i].id,&b[i].num);
62         build(1,n,1);
63         for(i=n-1;i>=0;i--){
64             update(b[i].id+1,b[i].num,1);
65         }
66     //    printf("11111\n");
67         out(1);
68         printf("%d",ans[1]);
69         for(i=2;i<=n;i++) printf(" %d",ans[i]);
70         printf("\n");
71     }
72 }
时间: 2024-10-30 16:36:26

POJ 2828 线段树(想法)的相关文章

poj 2828 线段树

http://poj.org/problem?id=2828 学到的思维: 1.变化的或者后来的优先影响前面的,那么从最后一个往前看,最后一个就成了 确定的, 并且后来的也可以确定----如果从前往后,所有的随时都不是确定的 2.线段树叶子节点直接维护区间(线段)信息,非叶子节点v维护的是以v为树根的整个子树的信息,那么假设父节点rt信息为[l,r]那么左子树维护[l,mid],右子树维护[mid+1,r]的信息.如果如果是前缀和,rt里是1-n的和,左子树1~n/2的和,右子树是n/2+1~n

poj 2828(线段树 逆向思考) 插队是不好的行为

http://poj.org/problem?id=2828 插队问题,n个人,下面n行每行a,b表示这个人插在第a个人的后面和这个人的编号为b,最后输出队伍的情况 涉及到节点的问题可以用到线段树,这里因为每个人插队时有顺序的,如果按照正着的顺序来情况太复杂,所以可以试试倒过来,从最后一个人开始,此时找到的位置 一定是最终位置,这样就很简单了,   结构体中多开一个mark表示每个区间的空位置,多开一个sum表示人的编号 这道题不错,提醒我们有时候换一换思路,逆向思考一下 1 #include<

poj 2828(线段树单点更新)

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 18561   Accepted: 9209 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 wa

poj 2828 线段树插孔处理

给你一个数列出现的先后顺序num[i]和对应数值   输出最后排好序的对应数值,如 4 0 77 1 51 1 33 2 69 第一步  77 第二部 77  51 第三步 77  33 51 第四部77  33  69 51 后面先出现的位置是固定的  所以从后往前处理. 线段树每个节点存当前区间还有多少个空位: #include<stdio.h> #include<string.h> #include<iostream> using namespace std; #

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

倒着插,倒着插,这道题是倒着插! 想一下如果 Posi 里面有若干个0,那么排在最前面的一定是最后一个0. 从后往前看,对于第i个数,就应该插在第Posi + 1个空位上,所以用线段树来维护区间空位的个数. 说一下那个坑爹的第56行的判断: if(i > 1) printf(" "); 将输出的n个数用空格隔开,我感觉这是一个还算常用的写法啊,结果各种莫名TLE,加上输入挂也补救不回来. 去掉这个无谓的判断后,3594MS险过,加上输入挂3094MS,还算是起到了一定的加速作用.

POJ 2828 线段树应用

这道题是之前一场比赛碰到的题目,当时看到题时以为是一道用链表优化的水题,交了几遍一直超时,简直不能再感人(┬_┬)今天有空突然想起去查了下题解,是用线段树做的.....完全想不到啊有木有~思路大概就是每个节点存当前节点下还剩多少空位,然后倒序查找该人所需要的位置,具体细节看代码吧. Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15801   Accepted: 7876 Description

Buy Tickets POJ - 2828线段树求插队

题意就是给你n个人,每个人有自己的要插入的pos和val,问你最后的排序 思路:逆序插入,最后一个人的位置一定是固定的,因为必须连续插入,所以第i个人插入的时候必须保证前面有pos[i]个空位, 那么用线段树记录位置个数,如果位置不够,就往后挪. #include<iostream> #include<cstdio> using namespace std; #define N 200005 int spare[N<<2]; int seq[N]; int pos[N]

POJ 2828 线段树单点更新 离线搞

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 ha

POJ 2570 线段树

Potted Flower Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description The little cat takes over the management of a new park. There is a large circular statue in