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

11 篇文章 0 订阅
2 篇文章 0 订阅

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值