Uva--10020(贪心)
2014-07-23 20:59:19
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.
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
思路:这题一开始想只扫一遍用O(n),后来怎么写还是WA QAQ,改成O(n^2)的算法就A了。后来用O(n)的算法又敲了一遍。
O(n^2):
1 /************************************************************************* 2 > File Name: Uva10020.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 23 Jul 2014 10:08:23 AM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 struct Seg{ 17 int l,r; 18 }s[100005],ans[100005]; 19 20 bool cmp(Seg a,Seg b){ 21 if(a.l == b.l) 22 return a.r < b.r; 23 return a.l < b.l; 24 } 25 26 int main(){ 27 //freopen("in","r",stdin); 28 int a,b,m,Case,cnt; 29 scanf("%d",&Case); 30 31 while(Case--){ 32 cnt = 0; 33 scanf("%d",&m); 34 while(scanf("%d%d",&a,&b) == 2 && (a || b)){ 35 s[cnt].l = a; 36 s[cnt].r = b; 37 ++cnt; 38 } 39 sort(s,s + cnt,cmp); 40 int ed,len = 0,acnt = 0; 41 while(1){ 42 if(len >= m) break; 43 ed = 0; 44 for(int i = 0; i < cnt; ++i){ 45 if(s[i].l <= len && s[i].r > len && s[i].r > ed){ 46 ed = s[i].r; 47 ans[acnt].l = s[i].l; 48 ans[acnt].r = s[i].r; 49 } 50 } 51 if(ed == 0){ 52 acnt = 0; 53 break; 54 } 55 len = ed; 56 ++acnt; 57 } 58 printf("%d\n",acnt); 59 for(int i = 0; i < acnt; ++i) 60 printf("%d %d\n",ans[i].l,ans[i].r); 61 if(Case) 62 printf("\n"); 63 } 64 return 0; 65 }
O(n):
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 6 struct Seg{ 7 int l,r; 8 }s[100005]; 9 10 bool cmp(Seg a,Seg b){ 11 return a.l < b.l; 12 } 13 14 int main(){ 15 int n,m,ans[100005],acnt,cnt; 16 int a,b; 17 scanf("%d",&n); 18 while(n--){ 19 scanf("%d",&m); 20 cnt = acnt = 0; 21 while(scanf("%d%d",&a,&b) == 2 && (a || b)){ 22 s[cnt].l = a; 23 s[cnt++].r = b; 24 } 25 sort(s,s + cnt,cmp); 26 int i = 0,st = 0,rmax = 0,flag; 27 while(s[i].r <= 0) ++i; 28 if(s[i].l > 0) printf("0\n"); 29 else{ 30 for(; i < cnt; ++i){ 31 flag = 0; 32 while(i < cnt && s[i].l <= st){ 33 if(s[i].r > rmax){ 34 rmax = s[i].r; 35 ans[acnt] = i; 36 } 37 flag = 1; 38 ++i; 39 } 40 if(flag) --i; 41 ++acnt; 42 st = rmax; 43 rmax = 0; 44 if(st >= m) break; 45 } 46 if(st >= m){ 47 printf("%d\n",acnt); 48 for(i = 0; i < acnt; ++i) 49 printf("%d %d\n",s[ans[i]].l,s[ans[i]].r); 50 } 51 else printf("0\n"); 52 } 53 if(n) printf("\n"); 54 } 55 return 0; 56 }