2018 Multi-University Training Contest 1--Distinct Values--hdu-6301

2018 ACM暑期多校联合训练 1 

Distinct Values

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3466    Accepted Submission(s): 1130

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≠ajholds.
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

Source

2018 Multi-University Training Contest 1

       这个题的题意呢,就是给你规定n长度的序列和m个区间,要求你构造这个序列的同时,要满足这m个区间中,每个区间的各个元素全不相同,且整个序列的字典序最小(就是让元素尽可能的小,可以理解为要求整个序列各个元素和相加和最小)。

比如第三个样例: 5 2   [1,3] [2,4]

首先构造 1-3 的区间,总和最小且不重复,所以为 1 2 3

然后构造 2-4 的区间,2 3 号位已经放了 2 3 了,第四个位置只要不为 2 和 3 即可,那么最放 1,最后没有区间限定的,可以直接放 1

所以答案为 1 2 3 1 1

 

       然后解题思路是使用贪心法,先将各个区间排序,就是尽可能让大区间排在前面,这样会减少很多时间,否则正常做会超时,其次使用优先队列储存可以使用的数字,这样排序时间也会减少。

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

struct node {
	int l, r;
} edge[100005];
int ans[100005];

int cmp(node a, node b) {
	if(a.l == b.l) {
		return a.r < b.r;
	}
	return a.l < b.l;
}

int main() {
	int t, a, b, x, y, numax, numin;
	scanf("%d", &t);
	while (t--) {
		memset(ans, 0, sizeof(ans));
		priority_queue<int, vector<int>, greater<int> >dong;
		queue<int>pu;
		numax = 999999;
		scanf("%d %d", &a, &b);
		for (int i = 1; i <= a; i++) {
			dong.push(i);
			ans[i] = -1;
			//printf("!!%d\n",q.top());
		}
		//printf("%d\n",q.top());
		for (int i = 1; i <= b; i++) {
			scanf("%d %d", &edge[i].l, &edge[i].r);
		}
		sort(edge+1, edge+b+1, cmp);
		int ml = 1, mr = 1;
		for(int i = 1; i <= b; i++) {
			if(edge[i].l != edge[i+1].l || i == b) {
				for(int j = ml; j < edge[i].l; j++) {
					if(ans[j] == -1) {
						ans[j] = 1;
					} else {
						dong.push(ans[j]);
					}
				}
				for(int j = mr; j <= edge[i].r; j++) {
					if(ans[j] == -1) {
						ans[j] = dong.top();
						dong.pop();
					}
				}
				ml = edge[i].l;
				mr = max(mr, edge[i].r);
			}
		}
		if(ans[1] == -1) {
			ans[1] = 1;
		}
		printf("%d", ans[1]);
		for(int i = 2; i <= a; i++) {
			if(ans[i] == -1) {
				ans[i] = 1;
			}
			printf(" %d", ans[i]);
		}
		puts("");
	}
}

然后这里还有一份大佬代码,先附上,有时间再来看看

#include <cstdio>
#include <set>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
const int N = 100100;
int t, n, m, pre[N], l, r, ret[N];
 
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &m);
		for (int i = 1; i <= n; i++) {
			pre[i] = i;
		}
		for (int i = 0; i < m; i++) {
			scanf("%d %d", &l, &r);
			pre[r] = min(pre[r], l);
		}
		for (int i = n-1; i >= 1; i--) {
			pre[i] = min(pre[i], pre[i+1]);
		}
		int pl = 1;
		set<int> val; // 表示当前可用数的集合
		for (int i = 1; i <= n; i++) {
			val.insert(i);
		}
		for (int i = 1; i <= n; i++) {
			while (pl < pre[i]) {
				val.insert(ret[pl]);
				pl++;
			}
			ret[i] = *val.begin();
			val.erase(ret[i]);
		}
		for (int i = 1; i <= n; i++) {
			if (i != 1) printf(" ");
			printf("%d", ret[i]);
		}
		puts("");
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值