ACM: 线段树 poj 2828 认真对待每…

来源:互联网 发布:mac安装jdk 编辑:程序博客网 时间:2024/05/16 06:35
Buy Tickets

Description

Railway tickets were difficult to buy around the Lunar NewYear in China, so we must get up early and join a longqueue…

The Lunar New Year was approaching, but unluckily the Little Catstill had schedules going here and there. Now, he had to travel bytrain to Mianyang, Sichuan Province for the winter camp selectionof the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from thenorthwest did not scare off the people in the queue. The cold nightgave the Little Cat a shiver. Why not find a problem to thinkabout? 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 tothe queue-jumpers. “If every person in the queue is assigned anintegral value and all the information about those who have jumpedthe queue and where they stand after queue-jumping is given, can Ifind out the final order of people in the queue?” Thought theLittle Cat.

Input

There will be several test cases in the input. Each test caseconsists of N + 1 lines where N (1 ≤ N ≤200,000) is given in the first line of the test case. The nextN lines contain the pairs of values Posiand Vali in the increasing order of i (1 ≤iN). For each i, the ranges and meanings ofPosi and Vali are asfollows:

  • Posi ∈ [0, i − 1] — The i-thperson came to the queue and stood right behind thePosi-th person in the queue. The booking officewas considered the 0th person and the person at the front of thequeue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th personwas assigned the value Vali.

There no blank lines between test cases. Proceed to the end ofinput.

Output

For each test cases, output a single line of space-separatedintegers which are the values of people in the order they stand inthe 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

 
题意: 农历新年, 排队买车票,天气寒冷, 天昏暗, 在排队的人们会出现插队(打尖)的现象, 现在
      给出先后顺序来排队的人,每个人又一个id, 和要插入的位置. 最后要输出队伍的全部id.
 
解题思路:
     1.线段树. 其实问题可以稍微变化一下, 由于每次插入队伍都是后面的人插入改变了队伍里面的
       先后顺序. 那么就可以"后来先得"的思想转变一下.
    2. 题目就可以变成: 把队伍先看成是空队列, 从后面往前面插入来的人, 每次来一个人就插入一个
       来排队的, 就相当于占满一个空位置.
    3. 线段树每一个节点就可以设置两个域来记录队伍的位置pos和剩余的空位置len.
       插入节点:
           (1)当时叶子节点时,result[ pt[pos].pos ] = id; (result是排队队列)
           (2)当插入的位置大于左孩子的最大位置时,insert(len-pt[pos*2].len, id, pos*2+1);
             插入右子树, 插入位置要减去左子树的空位置数.
           (3)否则:插入左子树 insert(len, id, pos*2);
 
代码:
#include<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 200005
struct point
{
 int id, pos;
}p[MAX];
struct node
{
 int l, r;
 int pos, len;
}pt[MAX*4];
int n;
int result[MAX];
void buildTree(intl, int r, int pos)
{
 pt[pos].l = l, pt[pos].r = r;
 pt[pos].len = (r-l+1);
 pt[pos].pos = 0;
 if(l == r)
 {
  pt[pos].pos = l;
  return ;
 }
 int mid = (l+r)/2;
 buildTree(l, mid, pos*2);
 buildTree(mid+1, r, pos*2+1);
}
void insert(intlen, int id, int pos)
{
 pt[pos].len--;
 if(pt[pos].l == pt[pos].r)
 {
  result[pt[pos].pos] = id;
  return ;
 }
 if(len >= pt[pos*2].len)
  insert(len-pt[pos*2].len, id,pos*2+1);
 else
  insert(len, id, pos*2);
}
int main()
{
// freopen("input.txt","r",stdin);
 while(scanf("%d",&n) !=EOF)
 {
  int i;
  for(i = 1; i <=n; ++i)
   scanf("%d%d",&p[i].pos, &p[i].id);
  buildTree(1, n, 1);
  for(i = n; i >=1; --i)
   insert(p[i].pos,p[i].id, 1);
  for(i = 1; i <=n; ++i)
  {
   if(i ==1)
    printf("%d",result[i]);
   else
    printf("%d",result[i]);
  }
  printf("\n");
 }
 return 0;
}
0 0
原创粉丝点击