Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1539 Accepted Submission(s): 792
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 (T≤1000).
Each test case starts with a line containing an integer N indicating the number of people in the queue (1≤N≤100000). Each of the next N lines consists of two integers hi and ki as described above (1≤hi≤109,0≤ki≤N−1). 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
Recommend
wange2014
题意: n个人排队,每一个人知道一个ki,表示前面比他高的人数或者后面比它高的人数,输出字典序最小的队伍排列。
分析:
因为要求字典序最小,所以我们先安排高度小的,让他们尽量靠前,所以对其从小到大排序,当我们安排到第i高人的位置时候,他前面或者后面有ki个高的人,所以i人位置有两种情况
1.当前面有ki个人比它高时,因为前面的人都比它小,所以要靠后面的来填,即得保证他的前面有ki个空位(空位不是位置)
2.当后面有ki个人比它高时,因为前面的人都比它小,所以要靠后面的来填,即还有n-i个比它高的人,后面有ki个,前面就有n-i-ki个,即得保证他的前面有n-i-ki个空位
注意到题目中,有不满足的情况,那怎么才不满足呢,肯定是n-i-ki<0,因为他就剩下n-i个比他高的人,如果n-i<ki,肯定不满足1、2两种情况。
位置到底选哪里,因为要求字典序最小,肯定要使得越矮的人越靠前,所以要保证前面空位数p=min(ki,n-i-ki)
怎么寻找有p个空位数的位置呢,二分+树状数组
树状数组我们维护长度为n的01序列,1代表该位置有空位,0代表被占领。
我们二分位置,sum(mid)就表示在mid之前有几个1,即多少空位,
#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int MAXN=200000+5;//最大元素个数
int n;//元素个数
int c[MAXN],ans[MAXN];//c[i]==A[i]+A[i-1]+...+A[i-lowbit(i)+1]
//返回i的二进制最右边1的值
int lowbit(int i)
{
return i&(-i);
}
//返回A[1]+...A[i]的和
int sum(int x){
int sum = 0;
while(x){
sum += c[x];
x -= lowbit(x);
}
return sum;
}
//令A[i] += val
void add(int x, int val){
while(x <= n){
c[x] += val;
x += lowbit(x);
}
}
int find_(int x)
{
int l=1,r=n,mid;
while(l<r)
{
mid=(l+r)>>1;
int num=sum(mid);
if(num<x)
l=mid+1;
else
r=mid;
}
return l;
}
struct node1
{
ll h,v;
}a[MAXN];
ll b[MAXN];
bool cmp(node1 x,node1 y)
{
return x.h<y.h;
}
int main()
{
int t;
t--;
cin>>t;
int kase=0;
while(t--)
{
kase++;
memset(c,0,sizeof(c));
memset(ans,0,sizeof(ans));
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
add(i,1);
scanf("%lld%lld",&a[i].h,&a[i].v);
}
sort(a+1,a+n+1,cmp);
int flag=1;
for(int i=1;i<=n;i++)
{
if(n-i-a[i].v<0) {flag=0;break;}
int p=min(a[i].v,n-i-a[i].v)+1; ///前面有多少个空位,包括本身,所以+1
int pos=find_(p);
add(pos,-1);
ans[pos]=a[i].h;
}
printf("Case #%d:", kase);
if (flag)
{
for (int i = 1; i <= n; i++) {
printf(" %d", ans[i]);
}
printf("\n");
}
else
printf(" impossible\n");
}
return 0;
}