UVA10020 - Minimal coverage
2013-03-07 16:25
375 查看
[b] Minimal coverage [/b] |
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 题目大意:给定N条线段,最少需要选择多少条线段可以覆盖区间[0,M]。 题解:贪心问题。对于输入的数据,我们可以过滤掉和区间[0,M]没有公共区间的线段,因为这些线段没有意义。之后按左端值对线段进行升序排序。假设s为需要覆盖区间的起点,如果区间1的左端点大于s,则说明问题无解,否则选择左端点小于等于s的右端点值最大的区间。选择区间[Li,Ri]之后,新的起点应该设置为Ri,并重复进行上述操作,直到遇到无法覆盖的情况或者已经覆盖完毕。我这题WA了三次,原因是每更新一次起点之后,忘记判断不能够再进行覆盖的情况,也就是说之后的所有点的左端点都在起点的右边,害得我一直循环,o(╯□╰)o。
View Code
#include<stdio.h> #include<string.h> #define MAXSN 100005 typedef struct { long x; long y; } NODE; NODE s[MAXSN]; NODE f[MAXSN]; void qsort(long l,long r) { long i,j,mid; NODE temp; i=l; j=r; mid=s[(l+r)>>1].x; while(i<=j) { while(s[i].x<mid) i++; while(s[j].x>mid) j--; if(i<=j) { temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } } if(i<r) qsort(i,r); if(j>l) qsort(l,j); } int main(void) { long i,j,k,m,n,ans,a,b,r,max,flag; NODE tt; scanf("%ld",&n); while(n--) { scanf("%ld",&m); ans=0; r=0; k=0; while(scanf("%ld%ld",&a,&b)==2) { if(a==0&&b==0) break; if((a<=0&&b<=0)||(a>=m&&b>=m)) continue; s[ans].x=a; s[ans].y=b; ans++; } qsort(0,ans-1); if(s[0].x>0) { printf("0\n"); if(n) printf("\n"); continue; } i=0; max=-1; j=0; flag=0; while(i<ans) { while(s[i].x<=r&&i<ans) { if(s[i].y>max) { tt=s[i]; max=s[i].y; } i++; } f[j++]=tt; r=max; if(r>=m) break; if(s[i].x>r) break; } if(r<m) printf("0\n"); else { printf("%ld\n",j); for(i=0; i<j; i++) printf("%ld %ld\n",f[i].x,f[i].y); } if(n) printf("\n"); } return 0; }
相关文章推荐
- uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
- uva 10020
- Minimal coverage - UVa 10020 贪心
- uva 10020 Minimal coverage 【贪心】+【区间全然覆盖】
- UVA 10020 简单贪心区间覆盖,各种WA,记录一下
- UVA 10020 Minimal coverage(最小覆盖问题)
- uva 10020 Minimal coverage(贪心-最小覆盖问题)
- UVA 10020 Minimal coverage
- UVA 10020 — Minimal coverage
- [算法入门经典] 区间最少覆盖问题 UVA 10020 - Minimal coverage
- UVa 10020 - Minimal coverage
- UVa 10020 - Minimal coverage
- UVa10020
- UVa 10020 - Minimal coverage(区间覆盖)
- [贪心]uva10020 Minimal coverage
- uva 10020
- uva 10020 Minimal coverage
- uva 10020 Minimal coverage
- UVA 10020 Minimal coverage 区间覆盖问题 贪心