HDU 6301 Distinct Values 思维

/**
D:Distinct Values
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6301
题意:给出m个区间,使得每个区间在长度为n的数组内不存在重复的数并使得该数组字典序最小;
分析:开始可能想复杂了许多,可能也是没有屡清思路吧;
由于区间是递增的,显然是可以从左往右进行贪心的;
对于区间不断的右移,窝们将l移动至a[i].l间时 ,也就是说在当前区间的数 都有可能为后面的数做贡献;
因此在l--->a[i].l移动时,将当前区间的ans[l] 用set维护一个最小值 而最小值就是r--->a[i].r区间所填充的数 
而set内的元素 正好和当前区间补齐(set+当前区间== 1--->n) 
*/

#include<bits/stdc++.h>
#define ll long long 
using namespace std;

const double eps=1e-6
const int maxn=1e5+7;
const int mod=1e9+7;

struct node{
	int l,r;
	bool operator <(const node &a)const {
		return (l==a.l&&r<a.r)||l<a.l;
	}
}a[maxn];

int ans[maxn];

int main (){
	int t;scanf("%d",&t);
	while(t--){
		int n,m;scanf("%d %d",&n,&m);
		for(int i=1;i<=m;i++) scanf("%d %d",&a[i].l,&a[i].r);
		for(int i=1;i<=n;i++) ans[i]=1;
		sort(a+1,a+1+m); set<int>s;
	    int l=a[1].l,r=a[1].r;
	    for(int i=1;i<=n;i++) s.insert(i);
	    for(int i=a[1].l;i<=a[1].r;i++) ans[i]=*s.begin(),s.erase(s.begin());
	    for(int i=1;i<=m;i++){
	    	while(l<a[i].l) s.insert(ans[l]),l++;
	    	r=max(a[i].l-1,r);// 存在区间 2 4   和  7 9 错开的区间保证r的复杂度  防止其被重复计算
 	    	while(r<a[i].r){
 	    		r++;
	    		ans[r]=*s.begin();
	    		s.erase(s.begin());
	    	}
	    }
	    for(int i=1;i<=n;i++) printf(i==n?"%d\n":"%d ",ans[i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hypHuangYanPing/article/details/81783110