Queue
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1481 Accepted Submission(s): 762
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
Source
2015 ACM/ICPC Asia Regional Hefei Online
这道题训练的时候压根没往线段树上想,肯定是从最矮的那个人入手,这点我倒是想到了,最矮的那个人有两种可能的位置,因为要字典序最小的,所以肯定选择靠左的位置,然后依次插入剩下的人,都是选择靠左的位置,用线段树维护一下就好了,当然每次要判断题目给的数据是否合法
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
#define lson num << 1
#define rson num << 1 | 1
struct node
{
int l,r;
int sum;
}tree[MAXN << 2];
int ans[MAXN];
struct per
{
int h,num;
}p[MAXN];
bool cmp(struct per a,struct per b)
{
return a.h < b.h;
}
void pushup(int num)
{
tree[num].sum = tree[rson].sum + tree[lson].sum;
}
void build(int num,int l,int r)
{
tree[num].l = l;
tree[num].r = r;
if(l == r) {
tree[num].sum = 1;
return;
}
int mid = (l + r) >> 1;
build(lson,l,mid);
build(rson,mid + 1,r);
pushup(num);
}
void update(int num,int pos,int val)
{
if(tree[num].l == tree[num].r) {
ans[tree[num].l] = val;
tree[num].sum = 0;
return;
}
if(pos <= tree[lson].sum) update(lson,pos,val);
else update(rson,pos - tree[lson].sum,val);
pushup(num);
}
int main(void)
{
int T,n,flag;
scanf("%d",&T);
int kase = 0;
while(T--) {
kase++;
scanf("%d",&n);
for(int i = 1; i <= n; i++) {
scanf("%d %d",&p[i].h,&p[i].num);
}
sort(p + 1,p + 1 + n,cmp);
build(1,1,n);
flag = 0;
for(int i = 1; i <= n; i++) {
if(n - i < p[i].num) {
flag = 1;
break;
}
if(n - i - p[i].num > p[i].num) {
update(1,p[i].num + 1,p[i].h);
}
else {
update(1,n - i - p[i].num + 1,p[i].h);
}
}
printf("Case #%d:",kase);
if(flag) printf(" impossible");
else {
for(int i = 1; i <= n; i++) {
printf(" %d",ans[i]);
}
}
printf("\n");
}
return 0;
}
/*
3
3
10 1
20 1
30 0
3
10 0
20 1
30 0
3
10 0
20 0
30 1
*/