uva10020 区间完全覆盖

Given several segments of line (int the X axis) with coordinates [Li, Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0, M].
Input
The first line is the number of test cases, followed by a blank line.
Each test case in the input should contains an integer M (1 ≤ M ≤ 5000), followed by pairs “Li Ri”
(|Li
|, |Ri
| ≤ 50000, i ≤ 100000), each on a separate line. Each test case of input is terminated by pair
‘0 0’.
Each test case will be separated by a single line.
Output
For each test case, in the first line of output your programm should print the minimal number of line
segments which can cover segment [0, M]. In the following lines, the coordinates of segments, sorted
by their left end (Li), should be printed in the same format as in the input. Pair ‘0 0’ should not be
printed. If [0, M] can not be covered by given line segments, your programm should print ‘0’ (without
quotes).
Print a blank line between the outputs for two consecutive test cases.
Sample Input
2
1
-1 0
-5 -3
2 5
0 0
1
-1 0
0 1
0 0
Sample Output
0
1
0 1

问题连接

问题描述

给出M是覆盖的区间的右端点,左端点为0,和一些区间的左右端点值,找出用最少的区间覆盖区间[0,M]。如果可以做到,输出用的最少的区间数和选用的区间的左右端点,具体格式看样例。如果无法实现,则输出0。

问题分析

贪心策略:在能添加区间的前提下,选择右端点最大的区间。
思路:为了选择最少的区间,那么就要求所选区间覆盖范围尽可能大,又要求区间完全覆盖,那么就必须要所选区间的左端点在上一个区间的中(或者在左端点的左边,以下操作使这个情况不用考虑)。如果我们从区间[0,M]的左边一步步覆盖,如某时已经覆盖到[0,a],那么对于下一个要选的区间[L,R],因要求区间完全覆盖,所以必须有0<=L<=a,为了使用的区间数小,那么只需要让R尽可能的大,就可以减少使用其它区间,因为在这个时候,a是确定的,无论L是大还是小,都对扩展区间范围没有起到贡献,所以很容易想到是对区间以R大的区间排在前面的规则进行排序。这样就解决了主要的两个问题:区间的连接和用最少的区间。
然后对区间一个个判断,选择合适的区间,直到区间被完全覆盖,或者判断到无法覆盖。
参考:https://blog.csdn.net/hyczms/article/details/39091779

代码如下


#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100005;
struct point {
	int l, r;
}pa[N];
bool cmp(point a, point b) {
	return a.r > b.r;
}

int main() {
	int t, m, i, count, num, end, choice[N];
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &m);
		count = 0;
		while (scanf("%d %d", &pa[count].l, &pa[count].r) != EOF) {
			if (pa[count].l == 0 && pa[count].r == 0)break;
			count++;
		}
		sort(pa, pa + count, cmp);
		end = 0;
		num = 0;
		while (end < m) {//循环跳出条件,说明覆盖完了
			for (i = 0; i < count; i++) {
			/*找合适的区间,也就是右区间端点尽可能大,且左端点在能连接到上一个选用的区间,
			由于排序方式是把右端点值大的放前面,所以在这里考虑的是第二个因素。*/
				if (pa[i].l <= end && pa[i].r > end) {//更新
					end = pa[i].r;
					choice[num++] = i;
					break;
				}
			}
			if (i == count) break;//说明再也无法找到合适的区间
		}
		if (end < m) {
			printf("0\n");
			continue;
		}
		printf("%d\n", num);
		for (i = 0; i < num; i++) printf("%d %d\n", pa[choice[i]].l, pa[choice[i]].r);
	}
	return 0;
}

感受

补这道题的时候,我花了很久的时间去想,但是仍是没有AC,后来看了很多别人的题解,选择了一个符合我思维习惯的,毕竟是学的别人的东西,最怕的就是知其然而不知所以然,如果只是学会怎么写这道题的代码,而不知道方法,那么即使是补了题,那么也是提升不高的。我认真地想了一下,之所以没想出来,应该是思维方式没跟上,一直在想能不能用一个循环找到所有要找的区间,因为总感觉两个循环会超时,其实上面那个算法就很好,虽然是两个循环,但循环过程中,只是一直在进行判断,实际的更新在一次循环中只进行一次,而且就算没有找到合适的区间也可以及时跳出。由于是将L大的排前面,所以合适的区间如果存在,那么在第二个循环体中找的时间一定比之前的短。
这个算法其实可以解决任意区间完全覆盖问题了,如果要求覆盖的区间是[a,b],那么只需要把初始的end赋值为a,m换成b,就行了。还有题解是按从区间的左边向右边覆盖,其实也可以按从右往左的方向覆盖,需要改一下排序方式,以l小的放前面;end改为start(命名习惯),初始start=m,循环条件(start>0);选择区间和上述规则相似,依据道理适当改变;然后注意逆向输出就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值