HDU - 5493(线段树)

NN people numbered from 1 to NN 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 ii-th person as hihi. The ii-th person remembers that there were kiki 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 kiki in a wrong direction. kiki could be either the number of taller people before or after the ii-th person. 
Can you help them to determine the original order of the queue? 

Input

The first line of input contains a number TT indicating the number of test cases (T≤1000T≤1000). 
Each test case starts with a line containing an integer NN indicating the number of people in the queue (1≤N≤1000001≤N≤100000). Each of the next NN lines consists of two integers hihi and kiki as described above (1≤hi≤109,0≤ki≤N−11≤hi≤109,0≤ki≤N−1). Note that the order of the given hihi and kiki is randomly shuffled. 
The sum of NN over all test cases will not exceed 106106 

Output

For each test case, output a single line consisting of “Case #X: S”. XX is the test case number starting from 1. SS 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

题目大意:

给出n个人的身高,和在他前面或后面比他高的人的数量。

输出这n个人的顺序,按字典序从小到大输出。不存在就输出impossible。

思路:

先把n个数的线段树建好。

把n个人按身高排序。

题目中有两个方向,就比较这个人位置的两种可能。然后再插入到线段树中。

如第i个人,前面有k个人比他高。则需空出k个位置,或者n-i-k个位置。比较之后,取小值。

代码:

 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
struct node
{
	int v, id;
}G[maxn];
int cmp(node a, node b)
{
	return a.v < b.v;
}
int sum[maxn * 5];
int W[maxn * 4];
int ans;int n;
void build(int l, int r, int rt)
{
	sum[rt] = r - l + 1;
	if (l == r)
	{
		W[rt] = 0;
		return;
	}
	int m = (l + r) / 2;
	build(l, m, rt << 1);
	build(m + 1, r, rt << 1 | 1);
}

void update(int id, int w, int l, int r, int rt)
{
	sum[rt]--;
	if (l == r)
	{

		W[rt] = w;
		return;
	}
	int m = (l + r) / 2;
	if (sum[rt << 1] > id)update(id, w, l, m, rt << 1);
	else update(id - sum[rt << 1], w, m + 1, r, rt << 1 | 1);
}

void go(int l, int r, int rt)
{
	if (l == r)
	{
		ans++;
		if (ans != n)printf("%d ", W[rt]);
		else printf("%d\n", W[rt]);

		return;
	}
	int m = (l + r) / 2;
	go(l, m, rt << 1);
	go(m + 1, r, rt << 1 | 1);
}
int main()
{
	int t;
	scanf("%d", &t);
	int T = 1;
	while (t--)
	{

		ans = 0;
		scanf("%d", &n);
		memset(sum, 0, sizeof(sum));
		memset(W, 0, sizeof(W));
		for (int i = 1;i <= n;i++)
		{
			int v, id;
			scanf("%d%d", &v, &id);
			G[i].v = v;G[i].id = id;
		}
		sort(G + 1, G + n + 1, cmp);
		build(1, n, 1);
		int flag = 0;
		for (int i = 1;i <= n;i++)
		{
			int id = G[i].id;
			int v = G[i].v;

			int id1 = id;
			int id2 = n - id - i;

			if (i + id > n)
			{
				flag = 1;
				break;
			}
			id = min(id1, id2);
			update(id, v, 1, n, 1);
		}
		printf("Case #%d: ", T++);
		if (!flag)
		{
			go(1, n, 1);
		}
		else
		{
			printf("impossible\n");
		}

	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值