您的位置:首页 > 其它

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

2016-05-19 23:24 423 查看
[align=center]Buy Tickets[/align]
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 ≤
iN). 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

 
题意: 农历新年, 排队买车票,
天气寒冷, 天昏暗, 在排队的人们会出现插队(打尖)的现象, 现在

      给出先后顺序来排队的人,
每个人又一个id, 和要插入的位置. 最后要输出队伍的全部id.

 
[align=left]解题思路:[/align]

     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);

 
[align=left]代码:[/align]
#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(int
l, 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(int
len, 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: