HDU 5493 Queue

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

2015 ACM/ICPC Asia Regional Hefei Online


给定人的身高,和排在他前面或后面比他高的人的个数。

构造一个序列使得满足,并且字典序最小。

用树状数组维护前缀和。

每次判断用二分找到一个人的位置。

复杂度(n*log(n));

#include<iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=100010;
int ans[maxn],sum[maxn];
struct node
{
    int h,x;
} a[maxn];
int n;
bool cmp(node aa,node bb)
{
    return aa.h<bb.h;
}
int low(int xx)
{
    return xx&(-xx);
}
int add(int i,int v)
{
    for(; i<=n; sum[i]+=v,i+=low(i));
}
int getsum(int i)
{
    int res=0;
    for(; i>0; res+=sum[i],i-=low(i));
    return res;
}
int main()
{
    int t,i;
    scanf("%d",&t);
    int dd=0;
    while(t--)
    {
        scanf("%d",&n);
        memset(ans,0,sizeof(ans));
        memset(sum,0,sizeof(sum));
        for(i=0; i<n; i++)
            scanf("%d%d",&a[i].h,&a[i].x);
        for(i=1; i<=n; i++)
            add(i,1);
        sort(a,a+n,cmp);
        printf("Case #%d:",++dd);
        int flag=0;
        for(i=0; i<n; i++)
        {
            int p=n-i;
            int q=a[i].x;
            if(p<=q)
            {
                flag=1;
                break;
            }
            int tmp=min(q,p-q-1)+1;
            int l=1;
            int r=n;
            while(l<r)
            {
                int mid=(l+r)>>1;
                if(getsum(mid)>=tmp)
                    r=mid;
                else
                    l=mid+1;
            }
            ans[r]=a[i].h;
            add(r,-1);
        }
        if(flag)
        {
            printf(" impossible");
        }
        else
        {
            for(i=1; i<=n; i++)
                printf(" %d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值