Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1553 Accepted Submission(s): 798
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
题意:
给你n(n<=1e5)个人的身高和其前面或者后面有多少比他高的人数,问你字典序最小的排队方式,如果不存在答案输出-1。
思路:
显然是线段树/树状数组搞。但是大佬用线段树很简单就搞过去了,我用树状数组调了两个多小时才调过。。。
大致思路是一样的,先把身高排序(假设从小到大,从大到小也可以)
然后从身高最低的开始,查找他前面的第sum+1(sum为其前面或者后面有多少比他高的人数)个空位的位置,和后面第sum+1个空位的位置,因为字典序最小,所以矮的优先往前放,即取这两个位置的最小值放这个人。
至于-1,假设身高从小到大第i名的人有超过n-i个比他高的人,那显然就是不合法情况了。
注意初始化。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=3e5+10;
ll c[maxn],cc[maxn];
ll n,m,k;
ll sum,ans,cnt,tmp;
ll as[maxn],nxt[maxn],ok[maxn];
struct node
{
ll h;
int sum;
int id;
bool operator<(node aa)const
{
return h<aa.h;
}
}a[maxn];
int lb(int x){return x&(-x);}
void add(ll *c,int x,ll v)
{
while(x<=n)
{
c[x]+=v;
x+=lb(x);
}
}
ll query(ll *c,int x)
{
int ans=0;
while(x>0)
{
ans+=c[x];
x-=lb(x);
}
return ans;
}
int findpos(ll *c,int sum)
{
//cout<<"* ";
int l=1,r=n,ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
int res=query(c,mid);
//cout<<mid-query(mid)<<" * "<<sum<<endl;
if(res==sum) r=mid-1;
else if(res<sum) l=mid+1;
else r=mid-1;
}
return l;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n+10;i++)
ok[i]=c[i]=cc[i]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld%d",&a[i].h,&a[i].sum);
a[i].id=i;
a[i].sum++;
}
sort(a+1,a+n+1);
int fg=1;
for(int j=1;j<=n;j++)
{
if(a[j].sum>n-j+1) {fg=0;break;}
}
for(int i=1;i<=n;i++)
{
add(c,i,1LL);
add(cc,i,1LL);
}
for(int i=1;i<=n&&fg;i++)
{
int pos1=findpos(c,a[i].sum);
int pos2=n-findpos(cc,a[i].sum)+1;
pos1=min(pos1,pos2);
ok[pos1]=a[i].h;
add(c,pos1,-1LL);
add(cc,n-pos1+1,-1LL);
//cout<<pos1<<endl;
}
printf("Case #%d:",cas++);
if(!fg) puts(" impossible");
else {
for(int i=1;i<=n;i++)
printf(" %d",ok[i]);
puts("");
}
}
return 0;
}