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

HDU 5493 Queue(线段树)

2015-11-13 21:52 435 查看

Queue

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 736 Accepted Submission(s): 396



[align=left]Problem Description[/align]
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 h_i. The
i-th person remembers that there were k_i 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 k_i in a wrong direction.
k_i 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?

[align=left]Input[/align]
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
h_i and k_i as described above (1≤h_i≤10^9,0≤k_i≤N-1). Note that the order of the given
h_i and k_i is randomly shuffled.

The sum of N over all test cases will not exceed
10^6

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

3
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1


[align=left]Sample Output[/align]

Case #1: 20 10 30
Case #2: 10 20 30
Case #3: impossible


[align=left]Source[/align]
2015 ACM/ICPC Asia Regional Hefei Online

#include <cstdio>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=1e5+10;
struct Node{
int h,x;
}node
;
int re
,sum[N*4];
bool cmp(Node a,Node b){
return a.h<b.h;
}
void Build(int now,int le,int ri){
if(le==ri)
{
sum[now]=1;  //这个点还没被用
return;
}
int mid=(le+ri)>>1;
int ne=now<<1;
Build(ne,le,mid);
Build(ne+1,mid+1,ri);
sum[now]=sum[ne+1]+sum[ne];   //这段区间有多少个点没被用
}
void Update(int now,int le,int ri,int pos,int val){
if(le==ri){
sum[now]=0;   //该点被使用
re[le]=val;   //插入位置
/*for(int i=1;i<=3;i++)
printf(" %d",re[i]);
printf("\n");*/
return;
}
int mid=(le+ri)>>1;
int ne=now<<1;
if(pos<=sum[ne])    //这个区间段未使用的点够,则进入
Update(ne,le,mid,pos,val);
else  //不够,则进入右子树,要减去前面左子树未使用的点,因为左子树在前面
Update(ne+1,mid+1,ri,pos-sum[ne],val);
sum[now]=sum[ne+1]+sum[ne];
}
int main(){
int t,i,n,j;
scanf("%d",&t);
for(j=1;j<=t;j++){
bool ok=1;
//	memset(sum,0,sizeof(sum));
//	memset(re,0,sizeof(re));
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d%d",&node[i].h,&node[i].x);
Build(1,1,n);
sort(node+1,node+n+1,cmp);
for(i=1;i<=n;i++){
int m=n-i;
//还有多少个比现在的高 因为前面的人矮  不能算 要n-i
int temp=m-node[i].x;
if(temp<0)
{
ok=0;
break;
}
//temp指后面方向高Node[i].x人的话的位置在哪
// Node[i].x指前面方向的话位置在哪;
//然后2个选靠前的 即更小的
if(node[i].x<=temp)
//在前面预留node[i].x人  插入到 node[i].x+1位置
//如果存在就一直往后推,这个在Update里实现
Update(1,1,n,node[i].x+1,node[i].h);
else
Update(1,1,n,temp+1,node[i].h);
}
printf("Case #%d:",j);
if(ok){
for(i=1;i<=n;i++)
printf(" %d",re[i]);
printf("\n");
}
else
printf(" impossible\n");
}
return 0;
}

/*
99
8
1 0
2 3
3 1
4 2
5 3
6 2
7 1
8 0
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: