Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1436 Accepted Submission(s): 735
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
题意:有n个人排成一列,给定每个人的身高和他前面或者后面身高比他高的人数,按输出最小字典序的可能排列
思路:和线段树插空的思路一样,只是要计算每个数插入的位置
①前面有k个比他高的人,前面要留出k个空位
②后面有k个比他高的人,当前插入了i个人,还剩余n-i个人没有插入,后面有k个,那前面就有n-i-k个人
要取以上两种情况的min作为插入的位置,若min小于0,则impossible
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define max_ 101000
#define inf 0x3f3f3f3f
#define mod 1000000007
using namespace std;
struct node
{
int l,r,w;
int sum;
};
struct nnode
{
int v,pos;
}num[max_];
struct node tree[max_*4];
int n,casnum=1;
bool cmp(const nnode &a,const nnode &b)
{
return a.v<b.v;
}
void built(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
if(l==r)
{
tree[i].sum=1;
return;
}
int mid=(l+r)>>1;
built(i<<1,l,mid);
built(i<<1|1,mid+1,r);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
void updata(int i,int cnt,int v)
{
if(tree[i].l==tree[i].r)
{
tree[i].w=v;
tree[i].sum--;
return;
}
if(tree[i<<1].sum>cnt)
updata(i<<1,cnt,v);
else
updata(i<<1|1,cnt-tree[i<<1].sum,v);
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
void show(int i)
{
if(tree[i].l==tree[i].r)
{
printf(" %d",tree[i].w);
return;
}
show(i<<1);
show(i<<1|1);
}
int main(int argc, char const *argv[]) {
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
built(1,1,n);
for(int i=1;i<=n;i++)
scanf("%d%d",&num[i].v,&num[i].pos);
sort(num+1,num+1+n,cmp);
int f=1;
for(int i=1;i<=n;i++)
{
int d=min(num[i].pos,n-i-num[i].pos);
if(d<0)
{
f=0;
break;
}
updata(1,d,num[i].v);
}
printf("Case #%d:",casnum++ );
if(f)
show(1);
else
printf(" impossible" );
printf("\n" );
}
return 0;
}