HDU-5493---Queue (线段树)

来源:互联网 发布:同和行知职业技术学校 编辑:程序博客网 时间:2024/05/21 06:36

Queue

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1391    Accepted Submission(s): 709



Problem Description
N people numbered from 1 to N are waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now, so they decide to go out and have lunch first. When they get back, they don’t remember the exact order of the queue. Fortunately, there are some clues that may help.
Every person has a unique height, and we denote the height of the i-th person as hi. The i-th person remembers that there were ki people who stand before him and are taller than him. Ideally, this is enough to determine the original order of the queue uniquely. However, as they were waiting for too long, some of them get dizzy and countedki in a wrong direction. ki could be either the number of taller people before or after the i-th person.
Can you help them to determine the original order of the queue?
 

Input
The first line of input contains a number T indicating the number of test cases (T1000).
Each test case starts with a line containing an integer N indicating the number of people in the queue (1N100000). Each of the next N lines consists of two integers hi and ki as described above (1hi109,0kiN1). Note that the order of the given hi and ki is randomly shuffled.
The sum of N over all test cases will not exceed 106
 

Output
For each test case, output a single line consisting of “Case #X: S”.X is the test case number starting from 1. S is people’s heights in the restored queue, separated by spaces. The solution may not be unique, so you only need to output the smallest one in lexicographical order. If it is impossible to restore the queue, you should output “impossible” instead.
 

Sample Input
3310 120 130 0310 020 130 0310 020 030 1
 

Sample Output
Case #1: 20 10 30Case #2: 10 20 30Case #3: impossible
 

Source
2015 ACM/ICPC Asia Regional Hefei Online
 

Recommend
wange2014


题意:t组数据,n个人,每个人有个身高h,和在他前面或者后面比他高的人数k,输出满足条件的最小字典序序列,没有满足条件的序列输出impossible;

思路:首先不考虑字典序最小,那么先把每个人按身高从小到大排序,每个人的位置就是他当前在从左往右的第k个空位或者从右往左的第k个空位,如果没有这么多空位则输出impossible,再考虑字典序最小则是选择每个数填在从左往右和从右往左的第k个位置靠左的那一个,这样确定策略之后就好办了,我们可以考虑用线段树维护当前空位的个数,然后填上数字之后单点更新。主要是难得想,写起来并不难~

AC代码:

#include<iostream>#include<cstring>#include<set>#include<map>#include<vector>#include<stack>#include<string>#include<algorithm>#include<queue>#include<cstdio>#define LL long longusing namespace std;priority_queue<LL,vector<LL>,greater<LL> > que,q;int t;struct node{    int val;    int id;}arr[100005];int brr[100005];bool cmp(node a,node b){    return a.val<b.val;}struct nn{    int l;    int r;    int sum;//空位数}segtree[100005*4];void pushup(int root){    segtree[root].sum=segtree[root<<1].sum+segtree[root<<1|1].sum;}void build(int root,int l,int r){    segtree[root].l=l;    segtree[root].r=r;    if(l==r)    {        segtree[root].sum=1;        return;    }    int mid=(l+r)>>1;    build(root<<1,l,mid);    build(root<<1|1,mid+1,r);    pushup(root);}int query(int root,int id){    int ll=segtree[root].l;    int rr=segtree[root].r;    if(ll==rr)        return ll;    int mid=(ll+rr)>>1;    if(segtree[root<<1].sum>=id)        return query(root<<1,id);    else        return query(root<<1|1,id-segtree[root<<1].sum);}void update(int root,int p){    int ll=segtree[root].l;    int rr=segtree[root].r;    if(ll==rr)    {        segtree[root].sum=0;        return;    }    int mid=(ll+rr)>>1;    if(p<=mid)        update(root<<1,p);    else        update(root<<1|1,p);    pushup(root);}int main(){    int t;    int cas=0;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=1;i<=n;i++)            scanf("%d%d",&arr[i].val,&arr[i].id);        sort(arr+1,arr+1+n,cmp);//按身高排序        build(1,1,n);        int flag =0;        for(int i=1;i<=n;i++)        {            int tmp=arr[i].id+1;//tmp为要填在第几个空位            if(arr[i].id+1>n-i+1-arr[i].id&&n-i+1-arr[i].id!=0)                tmp=n-i+1-arr[i].id;            if(segtree[1].sum<tmp)//没有这么多空格,直接结束            {                flag=1;                break;            }            int ans=query(1,tmp);            brr[ans]=arr[i].val;            update(1,ans);        }        printf("Case #%d: ",++cas);        if(flag)            printf("impossible\n");        else{            for(int i=1;i<=n;i++)            printf("%d%c",brr[i],i==n?'\n':' ');        }    }    return 0;}