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

hdu 5493 Queue(线段树)

2015-10-01 20:42 537 查看
[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 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?


[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 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


[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

题意:有n个人,每个人的身高和左边或右边比自己高的人的个数num[i],输出符合给出的条件且字典序最小的从左到右队列里每个人的身高。
题解:人有100000个,可以从线段树方法考虑,把问题转化为把每个人前面能留多少个空位给高个子的人,可以先按身高从小到大排个序,考虑第i个人前面留的位置肯定是num[i]或n-i-num[i]中的较小值,这样才能让字典序最小,一旦有n - i - num[i]的值小于0说明无解。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
struct Node{
int h,num;
}node
;
int n,s[N<<3],res
;

bool cmp(Node a,Node b){
return a.h<b.h;
}

void pushup(int k){
s[k]=s[k*2]+s[k*2+1];
}

void build(int k,int left,int right){
if(left==right){
s[k]=1;
return;
}
int mid=(left+right)/2;
build(k*2,left,mid);
build(k*2+1,mid+1,right);
pushup(k);
}

void modify(int k,int left,int right,int pos,int val){
if(left==right){
res[left]=val;
s[k]=0;
return;
}
int mid=(left+right)/2;
if(pos<=s[k*2]){
modify(k*2,left,mid,pos,val);
}else{
modify(k*2+1,mid+1,right,pos-s[k*2],val);
}
pushup(k);
}

int main()
{
int ac=0;
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&node[i].h,&node[i].num);
}
sort(node+1,node+1+n,cmp);
build(1,1,n);//建树

int flag=1;
for(int i=1;i<=n;i++){
int m=n-i;
int tmp=m-node[i].num;
if(tmp<0){
flag=0;
break;
}
if(node[i].num<tmp){
modify(1,1,n,node[i].num+1,node[i].h);
}else{
modify(1,1,n,tmp+1,node[i].h);
}
}
printf("Case #%d: ",++ac);
if(flag==0){
printf("impossible\n");
}
else{
printf("%d",res[1]);
for(int i=2;i<=n;i++){
printf(" %d",res[i]);
}
printf("\n");
}
}
return 0;
}


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