HDU 5493 线段树

来源:互联网 发布:树莓派安装unity3d 编辑:程序博客网 时间:2024/06/05 06:14

Queue

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1368    Accepted Submission(s): 693


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 (T1000).
Each test case starts with a line containing an integer N indicating the number of people in the queue (1N100000). Each of the next N lines consists of two integers hi and ki as described above (1hi109,0kiN1). 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
3310 120 130 0310 020 130 0310 020 030 1
 

Sample Output
Case #1: 20 10 30Case #2: 10 20 30Case #3: impossible
 
题意:有n个人排队,每个人有一个高度h,还有一个限制值num表示他前面或者后面要有 num 个比他高的人
让你在满足条件情况下按字典序最小排序。
思路:先让人按照高度从小到大排序,然后从小到大去给它安排位置
例如:样例  3     10 1 20 1 30 0
当前调用的人数据 为 h = 10 num = 1,那么说明他前面或后面需要留有 1个空位,因为后面进来的人都会比他高,我们就拿前面来留空位好了,用掉一个位置后还剩2个位置,因为后面都比它高,所以它必须在2位置
接下来的人数据为 h = 20 num = 1 ,那么它前或后需要一个空位,用掉自己的位置还剩1个空位,所以此时它放前放后都可以,为了字典序最小,所以它放前。
int cnt = n - i;
int tmp = cnt - a[i].num;
tmp = min(tmp,a[i].num);  表示可以放在剩下的第tmp个也可以放第a[i].num个,选小的放,使字典序最小

在update的时候,判断这个区间里够不够 need(需要空位),如果够的话就进入这个区间去放置数,不够的话则去另一个区间补,比如你需要5个空位,而左边的区间只有3个空位,那么你就得去右边区间找,此时右边区间需要的是need - tree[i << 1].sum  = 2个空位

这样使用线段树的做法相比你直接在数组里放数之后一个一个去找要快的多,而且不会浪费空间,他可以利用区间剩余空间 sum 巧妙地跳过空位找到他需要放置的地方。

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define mem(a,x) memset(a,x,sizeof(a));#define maxn 250005struct trie{int l,r,sum;}tree[maxn * 4];struct node{int h,num;}a[maxn * 4];int val[maxn * 4];bool cmp(node a,node b){return a.h < b.h;}void push_up(int root){tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;}void build(int i,int l,int r){      tree[i].l = l;      tree[i].r = r;      tree[i].sum = 1;      if(l == r)          return ;      int mid = (l + r) >> 1;      build(i << 1,l,mid);      build(i << 1 | 1,mid + 1,r);       push_up(i);}void update(int need,int v,int i){if(tree[i].l == tree[i].r){val[tree[i].l] = v;tree[i].sum = 0;return;}if(need <= tree[i << 1].sum){ //如果这个区间里的数足够多,那就放到这个区间里去 update(need,v,i << 1);}else{//不够的话则放到右边的区间去 update(need - tree[i << 1].sum,v,i << 1 | 1);}push_up(i);}int main(){int t,n,m,Case = 1;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i = 1;i <= n;i++){scanf("%d %d",&a[i].h,&a[i].num);}sort(a + 1,a + 1 + n,cmp);build(1,1,n);int flag = 0;for(int i = 1;i <= n;i++){int cnt = n - i;int tmp = cnt - a[i].num;tmp = min(tmp,a[i].num); //可以放在剩下的第tmp个也可以放第a[i].num个,选小的放,使字典序最小 if(tmp < 0){flag = 1;break;}update(tmp + 1,a[i].h,1);}printf("Case #%d: ",Case++);if(flag)puts("impossible");else{for(int i = 1;i <= n;i++){printf("%d",val[i]);if(i != n)printf(" ");}puts("");}}return 0;}



原创粉丝点击