【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
*/

 

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