1303. Minimal Coverage Given set of line segments [Li, Ri] with integer coordinates of their end points. Your task is to find the minimal subset of the given set which covers segment [0, M] completely (M is a positive integer). InputFirst line of the input contains an integer M (1≤m≤5000). Subsequent lines of input contain pairs of integers Li and Ri (−50000≤li < ri≤50000). Each pair of coordinates are placed on separate line. Numbers in the pair is separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.OutputYour program should print in the first line of output the power of minimal subset of segments which covers segment [0, M]. The list of segments of covering subset must follow. Format of the list must be the same as described on input with exception that ending pair of zeroes should not being printed. Segments should is printed in increasing order of their left end point coordinate.If There is no covering subset then print "no solution" to output.Samples
| input |
Output |
1-1 0-5-32 50 0 |
No Solution |
1-1 00 10 0 |
10 1
|
Input Mgive some line segments,ask at least how many bars can cover the interval [0,m],number of output barsthen press the left end point small to large to output these segments. maintain current right-most endpoint (first 0)
then find the line with the largest left endpoint that is smaller than this point and the right endpoint
Update the right endpoint Main View notes
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intmaxn=100000+4;8 9 structEdgeTen { One intL,r; A }EDGE[MAXN]; - - BOOLCMP (Edge A,edge B) the { - if(a.l==B.L) - returna.r<B.R; - returna.l<B.L; + } - + intANS[MAXN]; A at intMain () - { - intm; - - while(SCANF ("%d", &m)! =EOF) - { in - inttot=1; to +scanf"%d%d",&edge[tot].l,&EDGE[TOT].R); - the if(edge[tot].l==0&&edge[tot].r==0) * Break; $ Panax Notoginseng while(edge[tot].l| |EDGE[TOT].R) - { thetot++; +scanf"%d%d",&edge[tot].l,&EDGE[TOT].R); A } the +Sort (edge+1, edge+tot,cmp); - $ intnum=0; $ intCur=0; - intCnt=0; -edge[0].l=edge[0].r=-55555; the - BOOLflag=true;Wuyi the while(true) - { Wu if(cur>=m) - { About Break; $ } - - - //There's been a big mistake here. A //The code I just started with is this: + //While (CNT+1<TOT&&EDGE[CNT+1].L<=CUR&&EDGE[CNT+1].R>EDGE[CNT].R) the //cnt++; - //We're going to find out all the segments that meet the left endpoint on the left side of cur, $ //the farthest point of the right endpoint the //so use the variable right to represent the farthest current, the //and compare with right. the //Instead of comparing with the previous line segment. the - in intright=cur; the for(inti=cnt;i+1<tot;i++) the { About if(edge[i+1].l<=cur&&edge[i+1].r>Right ) the { thecnt=i+1; theright=EDGE[CNT].R; + } - } the Bayi if(edge[cnt].r<=cur) the { theflag=false; - Break; - } the the Else the { theans[num++]=CNT; -Cur=EDGE[CNT].R; the } the } the 94 if(!flag) the { theprintf"No solution\n"); the 98 Continue; About } - 101printf"%d\n", num);102 103 for(intI=0; i<num;i++)104 { theprintf"%d%d\n", EDGE[ANS[I]].L,EDGE[ANS[I]].R);106 }107 108 }109 the return 0;111}View Code
Timus 1303 Minimal Coverage DP or greedy