QueueTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 923 Accepted Submission(s): 484
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
Sample Output
|
题目大意:每一个人有自己的高度,每个人的前面或后面有k个人比自己高。求这个序列,若不行输出impossible。
解题思路:将每个人先按高度排序,然后当前有m个空位,当前这个人的k比m大则输出impossible,否则更min(k, m - k) + 1为这个人就行了。更新的时候利用线段树。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e5 + 3;
int T[4 * maxn];
int res[maxn];
struct people {
int num;
int hei;
}p[maxn];
bool cmp(people a, people b) {
return a.hei < b.hei;
}
void built(int l, int r, int q) {
if(r == l) {
T[q] = 1;
return ;
}
int mid = (l + r) / 2;
built(l, mid, q * 2);
built(mid + 1, r, q * 2 + 1);
T[q] = T[q * 2] + T[q * 2 + 1];
}
void modify(int l, int r, int q, int pos, int val) {
if(l == r) {
T[q] = 0;
res[l] = val;
return ;
}
int mid = (l + r) / 2;
if(pos <= T[q * 2]) {
modify(l, mid, q * 2, pos, val);
} else {
modify(mid + 1, r, q * 2 + 1, pos - T[q * 2], val);
}
T[q] = T[q * 2] + T[q * 2 + 1];
}
int main()
{
int TT;
scanf("%d", &TT);
for(int t = 1; t <= TT; ++t) {
int n;
scanf("%d", &n);
built(1, n, 1);
//for(int i = 1; i < 2 * n; ++i) cout << T[i] << "***" << endl;
for(int i = 1; i <= n; ++i) {
scanf("%d%d", &p[i].hei, &p[i].num);
}
sort(p + 1, p + n + 1, cmp);
bool flag = true;
for(int i = 1; i <= n; ++i) {
int m = n - i - p[i].num;
m = min(m, p[i].num);
if(m < 0) {
flag = false;
break;
}
modify(1, n, 1, m + 1, p[i].hei);
}
printf("Case #%d:", t);
if(flag) {
for(int i = 1; i <= n; ++i) {
printf(" %d", res[i]);
}
} else {
printf(" impossible");
}
printf("\n");
}
return 0;
}