链接:戳我。、
题目:
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更大的范围,因此选择第二条之后,结果不会变得更差。