hdu 5493 Queue 贪心+线段树

本文探讨了在特定约束条件下重建排队序列的问题,利用输入数据中的身高和相对位置信息,通过算法实现序列还原。

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

Queue

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


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


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

struct Node{
    int n,k;
};
int comp(Node a,Node b){
    return a.n < b.n;
}
#define maxn 400007
int res[maxn];
int n;
Node point[maxn];

int lc[maxn],rc[maxn],num[maxn];
int cnt = 0;

void build(int u,int l,int r){
    if(l == r) return ;
    int mid = (l+r)/2;
    lc[u] = cnt++;
    rc[u] = cnt++;
    build(lc[u],l,mid);
    build(rc[u],mid+1,r);
}

int query(int u,int l,int r,int k){
    if(l == r){
        num[u] = 1;
        return l;
    }
    int y = num[lc[u]];

    int ans = 0;
    int mid =(l+r)/2;
    int x = mid-l+1-y;
    if(x >= k)
        ans = query(lc[u],l,mid,k);
    else
        ans = query(rc[u],mid+1,r,k-x);
    num[u] = num[lc[u]]+num[rc[u]];
    return ans;
}

int work(){
    for(int i = 0;i < n; i++)
        scanf("%d%d",&point[i].n,&point[i].k);
    sort(point,point+n,comp);
    for(int i = 0;i < n; i++){
        int x = n - i - 1;
        if(point[i].k > x) return -1;
        point[i].k = min(point[i].k,x-point[i].k);
    }
    cnt = 1;
    memset(num,0,sizeof(num));
    build(0,0,n-1);
    for(int i = 0;i < n; i++){
        int p = query(0,0,n-1,point[i].k+1);
        res[p] = point[i].n;
    }
    return 0;
}

int main(){
    int t,tt=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int ans = work();
        printf("Case #%d:",tt++);
        if(ans == -1) printf(" impossible\n");
        else {
            for(int i = 0;i < n; i++)
                printf(" %d",res[i]);
            printf("\n");
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值