Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 497 Accepted Submission(s): 266
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?
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
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
//先排序 用二分查找位置 树状数组记录空位 一个个插入
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=100010;
struct Node
{
int num,i,k;
}p[maxn];
bool cmp(Node a,Node b)
{
return a.num<b.num;
}
int res[maxn],c[maxn],n;
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int x)
{
while(i<=n)
{
c[i]+=x;
i+=lowbit(i);
}
}
int sum(int i)
{
int sum=0;
while(i)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
int sear(int l,int r,int val)
{
int mid=0;
while(l<=r)
{
mid=(l+r)/2;
int t=mid-sum(mid);
if(t<val)
l=mid+1;
else
r=mid-1;
}
return l;
}
int main()
{
int t,tt=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(res,0,sizeof(res));
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i].num);
scanf("%d",&p[i].k);
}
sort(p+1,p+n+1,cmp);
int flag=0;
for(int i=1;i<=n;i++)
{
int pos=sear(1,n,p[i].k+1);
int pos1=sear(1,n,n-i-p[i].k+1);
if(pos>n||pos1<1)
{
flag=1;
break;
}
else
{
int t=min(pos,pos1);
res[t]=p[i].num;
add(t,1);
}
}
printf("Case #%d: ",tt++);
if(flag)
puts("impossible");
else
{
for(int i=1;i<=n;i++)
{
printf("%d%c",res[i],i==n?'\n':' ');
}
}
}
return 0;
}