HDU5493 Queue(线段树)

来源:互联网 发布:ec软件 编辑:程序博客网 时间:2024/05/29 18:53

Queue

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


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 counted ki 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 
 


题意:一个数列,针对每个数给出一个信息,告诉你该数左边或者右边比其大的数的个数,构造原数列,多解则输出字典序最小的。

思路:从小到大考虑,因为较小的数不会影响较大的数,每次放好一个数就把其位置删去,然后接着考虑即可。线段树维护前缀和。



#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <string>using namespace std;const long long INF=0xfffffffffffffff;const int MAXN=100010;struct node{    int x,h;}a[MAXN];int cmp(node x,node y){    return x.x<y.x;}int n;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[MAXN<<2];void pushup(int rt){    sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void build(int l,int r,int rt){    if(l==r){        sum[rt]=1;        return;    }    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt);}void update(int p,int l,int r,int rt){    if(l==r){        sum[rt]=0;        return;    }    int m=(l+r)>>1;    if(p<=m){        update(p,lson);    }else{        update(p,rson);    }    pushup(rt);}int query1(int x,int l,int r,int rt){    if(l==r){        if(sum[rt])            return l;        else            return -1;    }    int m=(l+r)>>1;    int y=sum[rt<<1];    if(x<=y){        return query1(x, lson);    }else{        return query1(x-y, rson);    }}int res[MAXN];int main(){    int t;    scanf("%d",&t);    int cas=1;    while(t--){        scanf("%d",&n);        for(int i=1;i<=n;i++){            scanf("%d%d",&a[i].x,&a[i].h);        }        sort(a+1,a+n+1,cmp);        build(1,n,1);        memset(res,0,sizeof res);        int cnt=n;        int ok=1;        for(int i=1;i<=n;i++){            int x1=query1(a[i].h+1, 1, n, 1);            int x2=query1(cnt-a[i].h, 1, n, 1);            if(cnt-1<a[i].h){                ok=0;                break;            }            if(x1==-1&&x2==-1){                ok=0;                break;            }            int pos;            if(x1!=-1&&x2!=-1){                x1=min(x1,x2);                res[x1]=a[i].x;                pos=x1;            }else if(x1==-1){                res[x2]=a[i].x;                pos=x2;            }else{                res[x1]=a[i].x;                pos=x1;            }            cnt--;            update(pos, 1, n, 1);        }        printf("Case #%d: ",cas++);        if(ok==0){            puts("impossible");            continue;        }        for(int i=1;i<=n;i++){            printf("%d%c",res[i],i==n?'\n':' ');        }    }}




原创粉丝点击