您的位置:首页 > 产品设计 > UI/UE

HDU 5493 Queue(二分+树状数组)

2016-09-16 00:54 429 查看
思路:显然的要字典序最小那么就离线先把身高排个序然后一个个插,对于某一个人来说,他的位置有可能在p[i].h+1,或者n-i-p[i].h+1这两个位置,然后二分前面有多少个位置来插入就可以了

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000+7;
int c[maxn],ans[maxn];
int lowbit(int x){return x&(-x);}
void update(int i,int v)
{
while(i<=maxn)
{
c[i]+=v;
i+=lowbit(i);
}
}
int query(int i)
{
int ans = 0;
while(i)
{
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
struct P
{
int h,pos;
}p[maxn];
bool cmp(P a,P b)
{
return a.h<b.h;
}

int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
memset(c,0,sizeof(c));
int n;
scanf("%d",&n);
for(int i = 1;i<=n;i++)
scanf("%d%d",&p[i].h,&p[i].pos);
sort(p+1,p+1+n,cmp);
int flag = 0;
for(int i = 1;i<=n;i++)
{
int k = min(p[i].pos,n-i-p[i].pos);
if(k<0)
{
flag=1;
break;
}
k++;
int l = 1,r=n;
int re = 0;
while(l<=r)
{
int mid = (l+r)>>1;
if(mid-query(mid)>=k)r=mid-1,re =mid;
else l = mid+1;
}
update(re,1);
ans[re]=p[i].h;
}
printf("Case #%d: ",cas++);
if(flag)
printf("impossible\n");
if(!flag)
for(int i = 1;i<=n;i++)
{
if(i==n)
printf("%d\n",ans[i]);
else
printf("%d ",ans[i]);
}
}
}


Description


 people
numbered from 1 to 

 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 

-th
person as 



.
The 

-th
person remembers that there were 



 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 



 in
a wrong direction. 



 could
be either the number of taller people before or after the 

-th
person. 

Can you help them to determine the original order of the queue? 

Input

The first line of input contains a number 

 indicating
the number of test cases (











). 

Each test case starts with a line containing an integer 

 indicating
the number of people in the queue (



















).
Each of the next 

 lines
consists of two integers 



 and 



 as
described above (

































).
Note that the order of the given 



 and 



 is
randomly shuffled. 

The sum of 

 over
all test cases will not exceed 





 

Output

For each test case, output a single line consisting of “Case #X: S”. 

 is
the test case number starting from 1. 

 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: