[Virtual Judge]URAL1303:Minimal Coverage
2013-07-25 18:16
337 查看
点击打开题目链接
Time limit: 1.0 second |
input | output |
---|---|
1 -1 0 -5 -3 2 5 0 0 | No solution |
1 -1 0 0 1 0 0 | 1 0 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; }
相关文章推荐
- URAL1303——贪心——Minimal Coverage
- URAL 1303
- Ural_1303
- URAL 1303
- Ural 1303 Minimal Coverage(贪心)
- Ural 1303 Minimal Coverage(贪心)
- ural 1303 Minimal Coverage【贪心】
- ural 1303 Minimal Coverage【贪心】
- [Virtual Judge]URAL1181:Cutting a Painted Polygon
- Minimal Coverage(ural1303,区间贪心)
- Ural 1303 Minimal Coverage(贪心)
- ural 1303 Minimal Coverage(贪心)
- Ural 1303 Minimal Coverage
- 13 URAL1303 Minimal Coverage
- 贪心 URAL 1303 Minimal Coverage
- 【贪心】URAL - 1303 Minimal Coverage
- URAL 1303 Minimal Coverage (贪心)
- URAL 1303
- virtual judge(专题一 简单搜索 B)
- URAL1004 Sightseeing Trip Floyd 最小环