码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2828 Buy Tickets

时间:2014-08-13 22:35:37      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   使用   os   io   strong   

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

bubuko.com,布布扣


题解及代码:


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <string>
#define maxn 200010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ALL %I64d
using namespace std;
typedef long long  ll;

struct node          //记录题目给的节点的信息
{
    int pos,value;
}nd[200010];
int pos[200010];   //记录叶子节点的位置,便于最后的输出
                   //因为最后所有的元素都是插入到叶子节点的

struct segment
{
    int l,r;
    int value;    //用来线段中剩余空间的数目
    int nd;       //叶子节点中用来记录插入的节点在nd结构体中的位置
} son[maxn<<2];

void PushUp(int rt)
{
    son[rt].value=son[rt<<1].value+son[rt<<1|1].value;
}

void Build(int l,int r,int rt)
{
    son[rt].l=l;
    son[rt].r=r;
    if(l==r)
    {
        pos[l]=rt;
        son[rt].value=1;
        return;
    }
    int m=(l+r)/2;
    Build(lson);
    Build(rson);
    PushUp(rt);
}


void Update(int w,int nd,int rt)   //查找插入位置并维护线段树
{
    if(son[rt].l==son[rt].r)
    {
        son[rt].value=0;           //查找到插入位置,设置次空间被占用
        son[rt].nd=nd;             //记录插入节点的位置
        return;
    }

    int m=(son[rt].l+son[rt].r)/2;

    if(son[rt<<1].value>w)
        Update(w,nd,rt<<1);
    else
    {
        w-=son[rt<<1].value;
        Update(w,nd,rt<<1|1);
    }
    PushUp(rt);
}

int main()
{
   int n;
   while(scanf("%d",&n)!=EOF)
   {
      Build(0,n-1,1);
      for(int i=n-1;i>=0;i--)
      {
          scanf("%d%d",&nd[i].pos,&nd[i].value);
      }
      for(int i=0;i<n;i++)
      {
          Update(nd[i].pos,i,1);
      }
      for(int i=0;i<n;i++)
      {
          if(i==0)
          printf("%d",nd[son[pos[i]].nd].value);
          else printf(" %d",nd[son[pos[i]].nd].value);
      }
      puts("");
   }
    return 0;
}
/*
对于第一个例子,我们分析一下,会发现:
4
0 20523
1 19243
1 3890
0 31492
它的最终结果是:31492 20523 3890 19243
当我们从后向前扫描的时候:每一个元素所占的位置就是后一个元素插入之后,
所剩空间中的绝对位置,为什么这么说?
当我们把31492插入之后,它的最终位置就是0,也就是1号位置,
当我们插入3890时,我们发现他最后的位置不是1,也就是2号位置,而是三号位置,
这是为什么呢?因为后面还有一个20523,它的优先级比3890要高,
所以当我们插入3890的时候,计算的是剩余空间的绝对位置,而不是所有空间的据对位置。

这里我们使用线段树维护每插入一个节点后,每条线短里面升入的空间,便于下次查找插
入的位置,每来一个数查找,插入,维护就可以了。

*转载请注明出处,谢谢。
*/






poj 2828 Buy Tickets,布布扣,bubuko.com

poj 2828 Buy Tickets

标签:des   style   http   color   使用   os   io   strong   

原文地址:http://blog.csdn.net/knight_kaka/article/details/38541419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!