【HDU - 6301】Distinct Values(思维+优先队列)

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

思路:

先按左端点排序,左端点相同右端点大的在前面。题意要求字典序最小,且给定区间内不能有重复元素,那么对于没有给定的区间都默认为1。用了一个优先队列,里面的数是这个区间内可以放的数。对于一个区间,首先枚举上一个区间,把不与当前区间重复的部分枚举一下,把不重复部分的值放回队列中,因为这些数在新的区间还可以再重新使用,然后枚举一遍没有赋值过的地方,然后从队列中取出一个数,放在那个位置。

数组开了2e5,T了,然后改成1e5过了

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int MAX=110000;
struct node{
	int l,r;
	friend bool operator<(node a,node b){
		if(a.l==b.l)
		return a.r>b.r;//?
		return a.l<b.l;
	}
}se[MAX];
int a[MAX];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		priority_queue<int,vector<int>,greater<int> > que;
		memset(a,-1,sizeof(a));
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++)
		scanf("%d%d",&se[i].l,&se[i].r);
		sort(se,se+m);
		for(int i=1;i<=n+10;i++) 
		que.push(i);
		for(int i=se[0].l;i<=se[0].r;i++)
		{
			int tm=que.top();
			que.pop();
			a[i]=tm;
		}
		int L=se[0].l,R=se[0].r;
		for(int i=1;i<m;i++)
		{
			if(se[i].l==L){
                continue;
            }
			if(se[i].r<=R) continue;
			int minn=min(se[i].l,R+1);
			int maxx=max(se[i].l,R+1);
			for(int j=L;j<minn;j++)
				que.push(a[j]);
			for(int j=maxx;j<=se[i].r;j++)
			{
				int tm=que.top();
				que.pop();
				a[j]=tm;
			}
			L=se[i].l;R=se[i].r;
		}
		for(int i=1;i<=n;i++)
		{
			printf("%d",(a[i]==-1)?1:a[i]);
			if(i==n)puts("");
			else printf(" ");
		}
	}
	
	return 0;
} 
/*
1
15 4
3 5
1 3
2 2
7 8
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章