杭电6301 Distinct Values

Problem Description

Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements ai and aj in the subarray al..r (l≤i<j≤r), ai≠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 T, indicating the number of test cases. For each test case:

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

It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.

Output

For each test case, output n 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

Source

2018 Multi-University Training Contest 1

这道题呢题意就是让你找长度为n的数组的序列,要求在给定区间上都要是最小子序列,并且序列中的值不能重复,可以用set集合来写

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
int pre[maxn];//记录每个区间的左界
int ans[maxn];
int n;
int main()
{
	int l, r;
	int a, b;
	scanf("%d", &n);
	while (n--)
	{
		scanf("%d%d", &a, &b);
		for (int i = 1; i <= a; i++) pre[i] = i;
		for (int i = 1; i <= b; i++)
		{
			scanf("%d%d", &l, &r);
			pre[r] = min(pre[r], l);//左界要最小
		}
		for (int i = a - 1; i >= 1; i--)
			pre[i] = min(pre[i], pre[i + 1]);
		int flag = 1;
		set<int>ss;
		for (int i = 1; i <= a; i++) ss.insert(i);
		for (int i = 1; i <= a; i++)
		{
			while (flag < pre[i])//flag要是比左界还要小的话,说明flag的值对ans数组没有影响,可以再次用
			{
				ss.insert(ans[flag]);//将flag的值送入set集合中
				flag++;
			}
			ans[i] = *ss.begin();
			ss.erase(ans[i]);
		}
		for(int i=1;i<=a;i++)
		{
			printf(i == a ? "%d\n" : "%d ", ans[i]);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/loven0326/article/details/81274221