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 wocould 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 shoshould 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 shocould 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), shocould be printed in the same format as in the input. pair "0 0" shocould not be printed. if [0, m] can not be covered by given line segments, your programm shold print "0" (without quotes ).
Print a blank line between the outputs for two consecutive test cases.
Sample Input
21-1 0-5 -32 50 01-1 00 10 0
Sample output
010 1
Cover (0, m) with the least interval.
Analysis: select a range that overwrites the destination range as much as possible. We can sort the leftmost endpoint of each given range, and select the maximum range of the right endpoint from the range with the left endpoint less than the st to be assigned to en. At this time, compare the size of St = EN and M, but not k> = M. Continue the above loop until it is satisfied, or the range that can be overwritten is not found. For details, see the code.
Code:
# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define M 100005 using namespace STD; struct node {int St, en ;} s [m]; int ans [m]; int CMP (node A, Node B) {if (. st = B. st) return. en> B. en; return. st <B. st;} int main () {int T, M; scanf ("% d", & T); While (t --) {scanf ("% d ", & M); int tot = 0, a, B; while (scanf ("% d", & A, & B), a | B) {If (A> B) Swap (a, B); s [tot]. st = A; s [tot]. en = B; ++ tot;} s ORT (S, S + tot, CMP); int St, en, num; ST = en = num = 0; while (ST <m) {en = sT; for (INT I = 0; I <tot; I ++) {If (s [I]. st <= sT & S [I]. en> en) {// from the left endpoint less than the st range, find the maximum value assigned to EN from the right endpoint, in addition, ANS [num] is used to store the subscript en = s [I] for the maximum range of the right endpoint. en; ans [num] = I ;}} if (en = sT) {// if not found, you can continue to overwrite the range and directly jump out. Num = 0; break;} ST = en; ++ num;} cout <num <Endl; For (INT I = 0; I <num; I ++) cout <s [ans [I]. st <"" <s [ans [I]. en <Endl;} exit (0 );}
Ultraviolet A 10020 minimal coverage [greedy] + [full range coverage]