杭电多校训练赛:D - Distinct Values(向左排序+set维护)

Chiaki has an array of nn positive integers. You are told some facts about the array: for every two elements aiai and ajaj in the subarray al..ral..r (l≤i<j≤rl≤i<j≤r), ai≠ajai≠aj holds. 
Chiaki would like to find a lexicographically minimal array which meets the facts. 

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case: 

The first line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105) -- the length of the array and the number of facts. Each of the next mm lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n). 

It is guaranteed that neither the sum of all nn nor the sum of all mm exceeds 106106. 

Output

For each test case, output nn integers denoting the lexicographically minimal array. Integers should be separated by a single space, and no extra spaces are allowed at the end of lines. 

Sample Input

3
2 1
1 2
4 2
1 2
3 4
5 2
1 3
2 4

Sample Output

1 2
1 2 1 2
1 2 3 1 1

【解析】

一开始用右边边界排序,死活过不去,然后瞎打了一组案例发现真的错了,然后用左边排序,并用set维护防止数字重复。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
struct node
{
	int be, en;
};
bool cmp(node a, node b)
{
	if (a.be == b.be)
		return a.en < b.en;
	else return a.be < b.be;
}
int main()
{
	int m, n, t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &m);
		//node *no = new node[m + 10];
		//int *ans = new int[n + 10];
		node no[maxn];
		int ans[maxn];
		set<int> se;
		for (int i = 0; i < m; i++)
			scanf("%d%d", &no[i].be, &no[i].en);
		sort(no, no + m, cmp);
		for (int i = 1; i <= n; i++)
		{
			se.insert(i);
			ans[i] = 1;
		}
		for (int i = no[0].be; i <= no[0].en; i++)
		{
			ans[i] = *se.begin();
			se.erase(se.begin());
		}
		int l = no[0].be;
		int r = no[0].en;
		for (int i = 1; i < m; i++)
		{
			if (no[i].be >= no[i - 1].be&&no[i].en <= no[i - 1].en)continue;
			while (no[i].be > l)
			{
				se.insert(ans[l++]);
			}
			while (no[i].en > r)
			{
				if (no[i].be <= r + 1)
				{
					ans[++r] = *se.begin();
					se.erase(se.begin());
				}
				else r++;
			}
		}
		/*int a = 1;//错误!错误!错误!
		for (int i = no[0].be; i <= no[0].en; i++)
			ans[i] = a++;
		for (int k = 1; k < m; k++)
		{
			a = 1;
			for (int i = no[k - 1].en + 1; i <= no[k].en; i++)
			{
				if (a != ans[no[k].be + a - 1])ans[i] = a++;
				else ans[i] = ans[i - 1] + 1;
			}
		}*/
		printf("%d", ans[1]);
		for (int i = 2; i <= n; i++)
			printf(" %d", ans[i]);
		puts("");
		//delete[] ans;
		//delete[] no;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值