hdu-5493 Queue(二分+树状数组)

队列重构问题
本文解决了一个有趣的问题:根据每个人的身高及他们记忆中前后比自己高的人数,重构银行队伍的原始顺序,并找到字典序最小的解。文章详细介绍了算法思路,包括数据预处理、关键参数调整以及使用树状数组进行高效查询与更新。

题目链接:

Queue

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


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个人的高度,以及这个人前面或者后面有多少个比她高,现在让你求字典序最小的队列的身高;

 

思路:

排序后可以得到这个队列中比某个人高的总人数假设是num,如果num<k比这个总人数还多的话就不可能了;

然后我们把这个k变成前边的比她高的人数,这个k是min(k,num-k)这样保证了字典序最小;

然后就是求每个位置上的身高呢,将身高从小大排序,这就是相当于给了一个序列的逆序数,然后让你还原这个序列了;

从小到大贪心,对于每个人二分她的位置,确定后更新到树状数组中,复杂度O(n*log2n);

 

AC代码:

#include <bits/stdc++.h>
using namespace std;  
typedef long long LL;
const int maxn=1e5+10;
int n,ans[maxn],sum[maxn],hi[maxn];
struct node
{
    int h,k,id;
}po[maxn];
int cmp(node a,node b){return a.h>b.h;}
int cmp1(node a,node b)
{
    if(a.h==b.h)return a.k<b.k;
    return a.h<b.h;
}
inline int lowbit(int x){return x&(-x);}
inline void update(int x)
{
    while(x<=n)
    {
        sum[x]++;
        x+=lowbit(x);
    }
}
inline int query(int x)
{
    int s=0;
    while(x)
    {
        s+=sum[x];
        x-=lowbit(x);
    }
    return s;
}
int main()  
{  
    int t,Case=0;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d:",++Case);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d%d",&po[i].h,&po[i].k),po[i].id=i;
        sort(po+1,po+n+1,cmp);
        int flag=0,num=0;po[0].h=-1;
        for(int i=1;i<=n;i++)
        {
            if(po[i].h!=po[i-1].h)num=i-1;
            if(po[i].k>num){flag=1;break;}
            hi[i]=num;
        }
        if(flag)printf(" impossible\n");
        else 
        {
            for(int i=1;i<=n;i++)po[i].k=min(po[i].k,hi[i]-po[i].k),sum[i]=0;
            sort(po+1,po+n+1,cmp1);
            for(int i=1;i<=n;i++)
            {
                int l=po[i].k+1,r=n;
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    if(mid-query(mid)>po[i].k)r=mid-1;
                    else l=mid+1;
                }
                ans[r+1]=po[i].h;
                update(r+1);
            }
            for(int i=1;i<=n;i++)printf(" %d",ans[i]);
            printf("\n");
        }
    }
    return 0;  
}  

  

转载于:https://www.cnblogs.com/zhangchengc919/p/5951261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值