HDU5493 Queue(线段树)

Queue

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


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
      
      
3 3 10 1 20 1 30 0 3 10 0 20 1 30 0 3 10 0 20 0 30 1
 

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

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5932  5931  5930  5929  5928 
题意: 有N个人排队,第i个人的身高为hi,他的前面或者后面有ki个比他高的人,求满足要求的最小
字典序序列,如果不存在则输出impossible。
思路:
先按身高从小到大排序,按照身高从小到大来确认每个人的位置。线段树里保存的是区间内位置
余量,假设第i个人的前面或后面比他高的人有ki个,那么用线段树求出满足前面后面余量刚好为
ki+1的位置,优先选择靠前的位置,然后更新线段树余量,当找不到满足条件的位置则
impossible(只要满足ki,那么放在前或后都是可以的,不影响是否是impossible)。
#include <bits/stdc++.h>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
#define maxn 100010
#define inf 1000000007
int tree[maxn<<2];
void pushUp(int rt){
    tree[rt] = tree[lson] + tree[rson];
}
void build(int rt, int l, int r){
    if(l == r){
        tree[rt] = 1;
        return;
    }
    int mid = (l+r)/2;
    build(lson, l, mid);
    build(rson, mid+1, r);
    pushUp(rt);
}
int query1(int rt, int l, int r, int val){
    if(l==r) return r;
    int mid = (l+r)/2;
    if(tree[lson]<val) return query1(rson, mid+1, r, val-tree[lson]);
    else return query1(lson, l, mid, val);
}
int query2(int rt, int l, int r, int val){
    if(l==r) return r;
    int mid = (l+r)/2;
    if(tree[rson]<val) return query2(lson, l, mid, val-tree[rson]);
    else return query2(rson, mid+1, r, val);
}
void update(int rt, int l, int r, int left, int right){
    if(left<=l&&r<=right){
        tree[rt] -= 1;
        return;
    }
    int mid = (l+r)/2;
    if(left<=mid) update(lson, l, mid, left, right);
    if(right>mid) update(rson, mid+1, r, left, right);
    pushUp(rt);
}
struct node{
    int h, k;
}a[maxn];
int ans[maxn];
bool cmp(const node &a, const node &b){
    return a.h<b.h;
}
int main()
{
    int T, N, i, cas;
    scanf("%d", &T);
    cas = 1;
    while(T--){
        scanf("%d", &N);
        for(i = 0;i < N;i++) scanf("%d %d", &a[i].h, &a[i].k);
        sort(a, a+N, cmp);
        //printf("%d\n", a[0].h);
        build(1, 1, N);
        bool flag = 1;
        int pos1, pos2;
        for(i = 0;i < N;i++){
            pos1 = pos2 = inf;
            if(tree[1]>=a[i].k+1){
                pos1 = query1(1, 1, N, a[i].k+1);
                pos2 = query2(1, 1, N, a[i].k+1);
            }
            if(pos1==inf&&pos2==inf){
                flag = 0;
                break;
            }//printf("%d %d %d\n", i, pos1, pos2);
            if(pos1<pos2){
                ans[pos1] = a[i].h;
                update(1, 1, N, pos1, pos1);
            }else {
                ans[pos2] = a[i].h;
                update(1, 1, N, pos2, pos2);
            }
            //printf("sum:%d\n", tree[1]);
        }
        printf("Case #%d: ", cas++);
        if(flag) for(i = 1;i <= N;i++) printf("%d%c", ans[i], i==N?'\n':' ');
        else printf("impossible\n");
    }
}


 
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值