Given several segments of line (int the X axis) with coordinates [Li, Ri]. You is to choose the minimal amount of them, such they would completely cover the segment [0, M].
Input
The first line was the number of test cases, followed by a blank line.
Each test case in the input should contains a 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 was terminated by pair ' 0 0 '.
Each test case is separated by a.
Output
For each test case, at 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 is printed in the same format as in the input. Pair ' 0 0 ' should not being
Printed. If [0, M] can is covered by given line segments, your programm should print ' 0 ' (without
Quotes).
Print a blank line between the outputs for the 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
Test instructions: Given a m, and some intervals [l,r]. To select several intervals to cover the [0,m] range completely. Minimum required quantity. If the output 0 cannot be overwritten.
Thought: The greedy thought. Sort the intervals by r from large to small. Then meet a satisfied [Li,ri] and update the narrowing interval. Until fully covered.
Note [L,r] only satisfies the condition that L is less than or equal and R is greater than the left end of the current coverage interval. To be selected.
#include <stdio.h>#include<algorithm>using namespacestd;intT;intstart,end,n,f;structqujian{intstart; intend;}; Qujian q[100005],p[100005];intCMP (Qujian A,qujian b) {returnA.end >b.end;}intMain () {scanf ("%d", &t); while(T--) {n=0; F=0; Start=0; scanf ("%d", &end); while(SCANF ("%d%d", &q[n].start, &q[n].end) && Q[n].start +q[n].end) {N++; } sort (Q,q+n,cmp); while(start<end) { inti; for(i=0; i<n;i++) { if(Q[i].start <= start && q[i].end >start) {Start= Q[i].end;//Update IntervalP[F] =Q[i]; F++; Break; } } if(i==n) Break;//If there is not an interval that satisfies the condition, it ends directly. } if(start<end) printf ("0\n"); Else{printf ("%d\n", F); for(intI=0; i<f;i++) printf ("%d%d\n", P[i].start,p[i].end); } if(t) printf ("\ n"); } return 0;}
UVa 10020-minimal Coverage (interval coverage and greed)