hdu 2828 Buy Tickets

2018-06-17 21:49:30来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

Buy Tickets

Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 31   Accepted Submission(s) : 15
Problem 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
<p>There will be several test cases in the input. Each test case consists of <i>N</i> + 1 lines where <i>N</i> (1 ≤ <i>N</i> ≤ 200,000) is given in the first line of the test case. The next <i>N</i> lines contain the pairs of values <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> in the increasing order of <i>i</i> (1 ≤ <i>i</i> ≤ <i>N</i>). For each <i>i</i>, the ranges and meanings of <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> are as follows:</p><ul><li><i>Pos<sub>i</sub></i> ∈ [0, <i>i</i> ? 1] — The <i>i</i>-th person came to the queue and stood right behind the <i>Pos<sub>i</sub></i>-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.</li><li><i>Val<sub>i</sub></i> ∈ [0, 32767] — The <i>i</i>-th person was assigned the value <i>Val<sub>i</sub></i>.</li></ul><p>There no blank lines between test cases. Proceed to the end of input.</p>
 

 

Output
<p>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.</p>
 

 

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
 

 

Source
PKU
 
 
还是线段树的新手,做完才恍然线段树只是可供利用的一种数据结构,具体怎么用要发挥自己的想象力。。
一开始还在想用lowbit()之类的常用函数。
这道题目的中心在,逆序插入的最后一人位置不变。在结点用ksum表示空位数量。
 
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include<vector>
#include<cstring>
using namespace std;
int a[200005],b[200005],ans[200005];
int tem,t;
struct node
{
    int l,r,ksum;
}tr[200005<<2];
void build(int n,int l,int r)
{
    tr[n].l=l;
    tr[n].r=r;
    tr[n].ksum=r-l+1;
    if(l==r)
        return;
    int m=(l+r)>>1;
    build(n<<1,l,m);
    build(n<<1|1,m+1,r);
}
void f(int k,int v,int n)
{
    int m=(tr[n].l+tr[n].r)>>1;
    if(tr[n].l==tr[n].r)
    {
        tr[n].ksum=0;
        ans[tr[n].l]=v;
        return;
    }
    if(tr[n<<1].ksum>=k)
        f(k,v,n<<1);
    else
        f(k-tr[n<<1].ksum,v,n<<1|1);
    tr[n].ksum=tr[n<<1].ksum+tr[n<<1|1].ksum;
}


int main()
{

    while(~scanf("%d",&t))
    {
        build(1,1,t);
        tem=0;
        for(int i=1;i<=t;i++)
            scanf("%d%d",&a[i],&b[i]);
        for(int i=t;i>0;i--)
            f(a[i]+1,b[i],1);
        for(int i=1;i<t;i++)
            cout<<ans[i]<<" ";
            cout<<ans[t]<<endl;
    }
}

  

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Doing Homework HDU - 1074

下一篇:Mirror Number SPOJ - MYQ10