hdoj 5493 Queue 【线段树 单点更新 + 区间查询】

解决一个排队问题,通过给定每个人的高度和相对位置线索,使用线段树数据结构来确定原始队伍顺序,确保找到的解是最小字典序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Queue

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


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
 



题意:N个人排队,由于吃午饭队伍散了。已知每个人的身高h(身高唯一),和一个数字num——代表 要么在他前面比他高的人有num个 要么在他后面比他高的人有num个。 现在让你找出一个满足约束的序列,若不存在输出impossible,反之输出字典序最小的序列。



思路:先按身高升序排列,排序后,对第i个插入的人,可以求出最优情况下他前面的人有min(N-i-num, num)个。

下面就是用线段树更新、维护、查询区间里面空位数,在每次插入前需要判断区间空位数是否少于num值。


AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 100000+1
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define INF 0x3f3f3f3f
using namespace std;
struct Tree
{
    int l, r;
    int sum;//剩余位置
};
Tree tree[MAXN<<2];
struct rec
{
    int h, num;
    int pos;
};
rec man[MAXN];
bool cmp(rec a, rec b){
    return a.h < b.h;
}
bool cmp1(rec a, rec b){
    return a.pos < b.pos;
}
void PushUp(int o){
    tree[o].sum = tree[ll].sum + tree[rr].sum;
}
void build(int o, int l, int r)
{
    tree[o].l = l;
    tree[o].r = r;
    tree[o].sum = 1;
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(o);
}
int query(int o, int v)
{
    if(tree[o].l == tree[o].r)
        return tree[o].l;
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(tree[ll].sum >= v)
        return query(ll, v);
    else
        return query(rr, v-tree[ll].sum);
}
void update(int o, int P)
{
    if(tree[o].l == tree[o].r)
    {
        tree[o].sum = 0;
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(P <= mid)
        update(ll, P);
    else
        update(rr, P);
    PushUp(o);
}
int main()
{
    int t, k = 1;
    scanf("%d", &t);
    int N;
    while(t--)
    {
        scanf("%d", &N);
        for(int i = 1; i <= N; i++)
            scanf("%d%d", &man[i].h, &man[i].num);
        sort(man+1, man+N+1, cmp);
        build(1, 1, N);
        bool flag = true;
        for(int i = 1; i <= N; i++)
        {
            if(N-i+1 <= man[i].num)
            {
                flag = false;
                break;
            }
            int Front = min(N-i-man[i].num, man[i].num);//最优状态下前面有多少个人
            int P = query(1, Front+1);
            update(1, P);
            man[i].pos = P;//记录位置
        }
        printf("Case #%d: ", k++);
        if(flag)
        {
            sort(man+1, man+N+1, cmp1);//按最优位置先后排序
            for(int i = 1; i <= N; i++)
            {
                if(i > 1) printf(" ");
                printf("%d", man[i].h);
            }
            printf("\n");
        }
        else
            printf("impossible\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值