POJ 2828

Buy Tickets

Time Limit: 4000MS  Memory Limit: 65536K

Total Submissions: 12734  Accepted: 6292

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

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.

<span style="color:#6600cc;">/***************************************************

      author   :   Grant Yuan
      time     :   2014.7.27
      algorithm:   线段树
      source   :   POJ 2828

****************************************************/

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAX  200005

using namespace std;
struct node{
int l;
int r;
int num;
};
node tree[3*MAX];
int p[MAX],v[MAX],ans[MAX];
int t,n;

void init()
{
    for(int i=0;i<=3*n;i++)
    {
        ans[MAX]=0;
    }
}

void build(int left,int right,int root)
{
  tree[root].l=left;
  tree[root].r=right;
  if(left==right)
  {
      tree[root].num=1;
      return;
  }
  int mid=(left+right)>>1;
  build(left,mid,root*2);
  build(mid+1,right,root*2+1);
  tree[root].num=tree[root*2].num+tree[root*2+1].num;
}

void update(int sum,int value,int root)
{
    if(tree[root].l==tree[root].r)
    {
        tree[root].num=0;
        ans[tree[root].l]=value;
        return;
    }
    if(sum<=tree[root*2].num)
        update(sum,value,root*2);
    else update(sum-tree[root*2].num,value,root*2+1);
   tree[root].num--;
}

int main()
{   int a,b;
    while(~scanf("%d",&n)){
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            p[i]=a+1;v[i]=b;
        }
        build(1,n,1);
        for(int i=n;i>0;i--)
        {
          update(p[i],v[i],1);
        }
        for(int i=1;i<=n;i++){
            if(i<n) printf("%d ",ans[i]);
            else  printf("%d",ans[i]);
            }
        printf("\n");
              }
    return 0;
}
</span>

POJ 2828,布布扣,bubuko.com

时间: 2024-11-05 13:40:20

POJ 2828的相关文章

poj 2828 Buy Tickets 【线段树点更新】

题目:poj 2828 Buy Tickets 题意:有n个人排队,每个人有一个价值和要插的位置,然后当要插的位置上有人时所有的人向后移动一位当这个插入到这儿,如果没有直接插进去. 分析:分析发现直接插入移动的话花时间太多,我们可不可以用逆向思维.从后往前来,因为最后一个位置是肯定能确定的,而其他的则插入空的第某个位置. 比如第一组样例: 4 0 77 1 51 1 33 2 69 开始时候位置都为空 编号0 1 2 3 首先从最后一个来2 69 第二个位置空,则可以直接放 然后编号变为0 1

poj 2828 Buy Tickets

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 13277   Accepted: 6595 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 Buy Tickets (线段树)

题目大意: 排队有人插队,每一次都插到第 i 个人的后面. 最后输出顺序. 思路分析: 你会发现,如果反向处理的话,你就知道这个人是第几个了. 那么问题一下子就简化了. 就是在线段树上找第几个空位置就行了. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <vector> #define lson num<<1

POJ 2828 Buy Tickets 线段树解法

此题应用线段树的方法非常巧妙.没做过真的难想得出是这么想的. 是一个逆向思维的运用. 其实一看到这道题目我就想到要运用逆向思维的了,但是就是没那么容易想通的. 思路: 1 要从后面往前更新线段树 2 线段树记录的是当前区间还剩下多少个记录空间 3 因为后面的插入会使得前面的插入往后退,那么前面插入的下标如果大于前面可以插入的空间,就需要往后退了. 好难理解的操作.仔细观察一下下面update这个函数吧. 二分地去查找适合的插入位置,把插入操作时间效率提高到 O(lgn),如果使用一般方法插入操作

线段树(单点更新) 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 poj 2828 Buy Tickets 【树状数组,已知前n项和为K,返回n值】

题目链接:http://poj.org/problem?id=2828 在一个队列中,一个人想要插队,告诉你每个新来的人会插在i个人后面,求出最后的队列. 如果我们用模拟的话,那么时间复杂度肯定是超了:想想,如果我们逆序,那么最后来的人的位置一定是固定的,这样的话,我们将问题转化成逆序扫描给出数据,插在i个人后面这个数据就变成了在这个人前面需要留出多少个空位.如此我们只需要用树状数组记录前n项总共有多少个空位,每扫描一个数据,就找出能使得他前面正好有i个空位. 这题用树状数组或者线段树都可以,今

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 Buy Tickets(神题!线段树or树状数组)

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

poj 2828 Buy Tickets(树状数组 | 线段树)

题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每个人站入的位置,以及这个人的权值,现在按队列的顺序输出每个人的权值. 解题思路:第K大元素,很巧妙,将人入队的顺序倒过来看,就是纯第K大问题,然后用树状数组还是线段树就都可以做了. C++ 线段树 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn