HDU 5493 Queue (树状数组+二分)2015 ICPC 合肥网赛

Queue

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


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个人的身高和他前面或者后面身高大于他的人的个数,求一个字典序最小的满足此条件的序列,如果不存在输出“impossible”。

分析:我们从字典序最小入手,如果一个位置可以放多个数,那么肯定放最小的,所以我们从小到大处理,并且小的数不影响大的数的放置,因为第一个值是比当前人身高大的人的个数。那么我们考虑当前数(保证处理的数是未处理集合中最小的那个),可以存放的位置,并将他放置在最前面以保证字典序最小,这样依次处理完n个数最后用一个Max大小的数组记录每个人的位置即可。

比如这组样例:(为了方便说明我按升序输入)

8

1 0

2 3

3 1

4 2

5 3

6 2

7 1

8 0

用一个数组存储位置,初始时为_ _ _ _ _ _ _ _

第一次选择最小的存放,为1,因为1的权值为0,所以我们不用给1留出空位,保证字典序最小那么放在第一个位置,此时结果为 _ _ _ _ _ _ _

第二次选择2,权值为3,那么就要保证2的左边或者右边有3个空位存放比它大的,保证字典序,此时结果为1 _ _ _ 2 _ _ _ 

第三次选择3,权值为1,同上,此时结果为1 _ 3 _ 2 _ _ _ 

第四次选择4,权值为2,同上,结果为1 _ 3 _ 2 4 _ _

第五次5,结果为1 5 3 _ 2 4 _ _

第六次6,结果为1 5 3 6 2 4 _ _

第七次第八次...

最终结果为 1 5 3 6 2 4 7 8

由于空位的个数不好找,更不好更新,所以我们采用树状数组来维护,二分查找。总的复杂度为O(nlogn)。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf 0x3f3f3f3f
#define ll long long int
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
const int mod = 1000000007;
const int Max = 100005;
int ans[Max],sum[Max];
int t,n,cnt=1;
struct node
{
    int x,v;
}a[Max];
bool cmp(node u,node v){return u.x<v.x;}
int lowb(int t){return t&(-t);}
void add(int i,int v)
{
    for(;i<=n;sum[i]+=v,i+=lowb(i));
}
int gsum(int i)
{
    int s=0;
    for(;i>0;s+=sum[i],i-=lowb(i));
    return s;
}
int main()
{
    cin>>t;
	while(t--)
    {
        memset(ans,0,sizeof ans);
        memset(sum,0,sizeof sum);
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d",&a[i].x,&a[i].v);
        for(int i=1;i<=n;i++)
            add(i,1);
        sort(a,a+n,cmp);
        printf("Case #%d: ",cnt++);
        int flag=0;
        for(int i=0;i<n;i++)
        {
            int p=n-i;
            int q=a[i].v;
            if(p<=q){flag=1;break;}
            int tmp=min(q+1,p-q);
            int l=1,r=n;
            while(l<r)
            {
                int m=(l+r)>>1;
                if(gsum(m)>=tmp) r=m;
                else l=m+1;
            }
            //cout<<r<<" "<<a[i].x<<endl;
            ans[r]=a[i].x;
            add(r,-1);
        }
        if(!flag)
        {
            for(int i=1;i<=n;i++)
            {
                if(i!=n)
                    printf("%d ",ans[i]);
                else
                    printf("%d\n",ans[i]);
            }
        }
        else
            puts("impossible");
    }
    return 0;
}

题目链接: 点击打开链接




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值