UVA 10020 - Minimal coverage

本文介绍了一种解决最小覆盖区间问题的方法。该问题要求从给定的线段中选择最少数量的线段来完全覆盖指定区间[0,M]。文章详细阐述了解决方案的步骤,并通过证明解释了为什么这种方法总是能找到最优解。

链接:戳我。、

题目:

Minimal Coverage

The Problem

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].

The 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.

The 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.

 

 解答:

做法:

不失一般性,我们假设要求cover的线段是[A, M]

首先,对Ri进行从小到大排序

然后,在所有 Li < A and Ri > B 的线段中,选择Ri最大的那一条。

之后,把原先要求cover的线段设为[Ri, M],重复第一步。

 

证明:

对于第一条线段,这条必定是能cover A这个点的,假设在最优解里面,第一条线段不是我们选择的这一条,那么我们把第一条线段换成我们现在选的这一条,不会使得结果变差,因此我们这么选择是正确的。

对于第二条线段,假设在最优解里面,第二条线段不是我们现在选择的这一条。现在我们交换我们选择的和最优解的第一条线段,由于第二条我们选择的线段能够cover更大的范围,因此选择第二条之后,结果不会变得更差。

转载于:https://www.cnblogs.com/frankdj412/archive/2013/03/19/2970153.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值