[Virtual Judge]URAL1303:Minimal Coverage
来源:互联网 发布:unity3d 百度三维地图 编辑:程序博客网 时间:2024/04/30 11:20
点击打开题目链接
1303. Minimal Coverage
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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).
Input
First 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 is placed on separate line. Numbers in the pair are separated with space. Last line of input data contains a pair of zeroes. The set contains at least one and at most 99999 segments.
Output
Your 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 in input with exception that ending pair of zeroes should not be printed. Segments should be printed in increasing order of their left end point coordinate.
If there is no covering subset then print “No solution” to output.
Samples
1-1 0-5 -32 50 0
No solution
1-1 00 10 0
10 1
=====================================题目大意=====================================
使用最少的提供的区间覆盖指定区间。
=====================================算法分析=====================================
贪心算法(此题即刘汝佳白书P154:区间覆盖问题,但是下面代码中我使用的算法与白书不同)。
=======================================代码=======================================
#include<cstdio>#include<algorithm>using namespace std;int M,InterSum,AnsID[100005],CntAns; struct Interval { int L,R; } Inter[100005];bool cmp(Interval& I1,Interval& I2) {return I1.L<I2.L;}bool Greedy(){CntAns=0;sort(Inter,Inter+InterSum,cmp);int i=0,CurCoverEnd=0;while(i<InterSum&&CurCoverEnd<M){int Best;for(Best=-1;i<InterSum;++i){if(Inter[i].R<CurCoverEnd) { continue; }if(Inter[i].L>CurCoverEnd) { break; }if(Best==-1||Inter[Best].R<Inter[i].R) { Best=i; }}if(Best==-1) { break; }CurCoverEnd=Inter[Best].R; AnsID[CntAns++]=Best; }return M<=CurCoverEnd;}int main(){while(scanf("%d",&M)==1){InterSum=0;while(1){scanf("%d%d",&Inter[InterSum].L,&Inter[InterSum].R);if(Inter[InterSum].L==0&&Inter[InterSum].R==0) { break; }++InterSum;}if(Greedy()==true){printf("%d\n",CntAns);for(int i=0;i<CntAns;++i){printf("%d %d\n",Inter[AnsID[i]].L,Inter[AnsID[i]].R);}}else{ printf("No solution\n");}}return 0;}
- [Virtual Judge]URAL1303:Minimal Coverage
- 13 URAL1303 Minimal Coverage
- Minimal Coverage(ural1303,区间贪心)
- 10020 - Minimal coverage
- UVa 10020 - Minimal coverage
- UVa 10020 - Minimal coverage
- uva 10020 - Minimal coverage
- uva 10020 - Minimal coverage
- 10020 - Minimal coverage
- uva10020 - Minimal coverage
- UVA 10020 Minimal coverage
- UVaOJ10020 - Minimal coverage
- UVA 10020 - Minimal coverage
- UVA 10020 Minimal coverage
- Timus 1303. Minimal Coverage
- UVa:10020 Minimal coverage
- uva 10020 Minimal coverage
- uva 10020 - Minimal coverage
- 黑马程序员---6、内部类(InnerClass)和常用类
- 用户流失原因调研四步经
- Struts2爆高危漏洞殃及大量网站
- T-SQL查询进阶-10分钟理解游标
- SB学习
- [Virtual Judge]URAL1303:Minimal Coverage
- 各种字符编码介绍
- Android获取内存信息 .
- gdb 调试高级命令
- 才储分析
- hadoop日志【3】---进度不动的reduce过程
- java.lang.IllegalStateException: Cannot forward after response has been committed
- 字符编码笔记:ASCII,Unicode和UTF-8
- @cache注释驱动的 Spring cache 缓存介绍