HDU-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

题意:

找一个长度为n字典序最小的序列,使得m个区间,每个区域间内没有相同数值

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
	    int n,m;
		scanf("%d%d",&n,&m);
		vector<int>ends(n,-1);
        set<int>unused;
		for(int i=0;i<n;i++)
		   ends[i]=i;
		for(int i=0;i<m;i++){
			int l,r;
			scanf("%d%d",&l,&r);
			ends[l-1]=max(ends[l-1],r-1);
		}   
		for(int i=1;i<=n;i++)
		   unused.insert(i);
	    vector<int>ret(n);
	    int l=0,r=-1;
		for(int i=0;i<n;i++){
			if(r>=ends[i]) continue;
			while(l<i){
				unused.insert(ret[l++]);
			}
			while(r<ends[i]){
				ret[++r]=*unused.begin();
				unused.erase(ret[r]);
			}
		}
		for(int i=0;i<n;i++){
			if(i) printf(" ");
			printf("%d",ret[i]);
		}
		puts("");	     
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81188489