杭电多校训练赛: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;
}

猜你喜欢

转载自blog.csdn.net/waterboy_cj/article/details/81346531