poj2828 Buy Tickets

来源:互联网 发布:windows计划任务设置 编辑:程序博客网 时间:2024/05/21 17:57

Buy Tickets
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 19422 Accepted: 9616

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

40 771 511 332 6940 205231 192431 38900 31492

Sample Output

77 33 69 5131492 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.


题意:买票,可能会插队,告诉你来的人的插入的位置和数值。让你把最后的数值输出来。

思路:想了好久,没想到有什么好解决办法,看了别人的一时半会也不知道看空位数是什么意思。

后来算是慢慢的想明白了。

首先就是,最后插入的那个人位置一定是固定的,所以我们从后面往前插。至于为什么是插空位,我不知道是谁想出来的,确实能过这个题。

看一下它告诉你的事插入的位置和数值。我们重点研究一下插入的位置。我们知道你相同的位置后来的必定是向后走的。

给你一个位置,我们看一下当前区间的空位数,如果插入的位置比当前区间的空位数小,那么往左区间插,(因为左区间的位置小),否则就往右区间插。

注意如果往右区间插,那么是必须要减去左区间的空位的个数的。

因为是倒着来的,所以后面来的位置是固定的,因此我们根据空位数和位置的关系,来找到最终的位置,最后得出答案。

模拟的过程其实就是我们从最后一个开始,确定下位置,扣去,相当于不存在了,剩下的n-1个位置重新排好,然后重复上一步。因为下一个不存在的话对于上一个来说就是当前的位置,是固定的了,就是因为下一个来了,才有可能变动的。而扣去之后的位置的体现其实就是空位的位置。

然后用线段树的话就能快速定位位置了。

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int MAXN=2e5+7;int n,m;int ans[MAXN];struct node{    int l,r;    int sum;}tree[MAXN<<2];int pos[MAXN],x[MAXN];void build_tree(int i,int l,int r){    tree[i].l=l;    tree[i].r=r;    tree[i].sum=r-l+1;    if(l==r)return;    int mid=(l+r)>>1;    build_tree(i<<1,l,mid);    build_tree(i<<1|1,mid+1,r);}int get_ans(int i,int val){    if(tree[i].l==tree[i].r)    {        tree[i].sum=0;        return tree[i].l;    }    int p;    if(val<tree[i<<1].sum)p=get_ans(i<<1,val);//这里用小于主要是因为标号是从0开始的。    else p=get_ans(i<<1|1,val-tree[i<<1].sum);    tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;    return p;}int main(){    while(~scanf("%d",&n))    {        build_tree(1,0,n-1);        for(int i=0;i<n;++i)        {            scanf("%d%d",&pos[i],&x[i]);        }        for(int i=n-1;i>=0;--i)        {            ans[get_ans(1,pos[i])]=x[i];        }        for(int i=0;i<n-1;++i)printf("%d ",ans[i]);        printf("%d\n",ans[n-1]);    }    return 0;}





1 0
原创粉丝点击