《黑书》uva10020最少区间覆盖(贪心)
2013-05-19 18:07
253 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=12&problem=961&mosmsg=Submission+received+with+ID+11784999
题意:有n段区间,用这n段区间去覆盖区间为0~~m的区间,求最少的区间段数。
思路:把区间记为【xi,yi】;那么思考,只要按照xi从小到达排序,如果第一个区间起点不包含0就是无解的,否则则选择包含起点的最长区间,选择完后,新的起点就是yi了,在选择下一段区间......
代码:
题意:有n段区间,用这n段区间去覆盖区间为0~~m的区间,求最少的区间段数。
思路:把区间记为【xi,yi】;那么思考,只要按照xi从小到达排序,如果第一个区间起点不包含0就是无解的,否则则选择包含起点的最长区间,选择完后,新的起点就是yi了,在选择下一段区间......
代码:
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct ss { int x,y; }s[100000]; int cmp(const ss a,const ss b) { if(a.x<b.x) return 1; else if(a.x==b.x&&a.y>b.y) return 1; else return 0; } int main() { int text,a[100000],b[100000]; scanf("%d",&text); while(text--) { int m,len=0; scanf("%d",&m); while(1) { int tmp,tmp1; scanf("%d%d",&tmp,&tmp1); if(tmp==0&&tmp1==0) break; s[len].x=tmp; s[len].y=tmp1; len++; } sort(s,s+len,cmp); int maxx=0,sum=0,pos=0; while(1) { if(pos>=m) break; maxx=0; for(int i=0;i<len;i++) if(s[i].x<=pos&&s[i].y>pos) { if(maxx<s[i].y) { maxx=s[i].y; a[sum]=s[i].x; b[sum]=s[i].y; } } if(maxx==0) { sum=0; break; } pos=maxx; sum++; } printf("%d\n",sum); for(int j=0;j<sum;j++) printf("%d %d\n",a[j],b[j]); if(text) printf("\n"); } return 0; }
相关文章推荐
- UVa 10020 - Minimal coverage(区间覆盖、贪心)
- UVA 10020 Minimal coverage 区间覆盖问题 贪心
- uva 10020 Minimal coverage(贪心,区间覆盖)
- uva 10020- Minimal coverage (贪心思想 简单区间覆盖)
- uva 10382 贪心 区间覆盖问题
- UVa 10382 - Watering Grass (贪心—区间覆盖)
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
- uva 10382 Watering Grass(贪心-区间覆盖变形)
- uva 10382 Watering Grass(贪心-区间覆盖变形)
- UVa 10382 - Watering Grass(贪心+区间覆盖问题)
- UVA - 10382 Watering Grass 贪心+区间覆盖
- UVA 10382 Watering Grass(贪心 区间覆盖问题)
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
- uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】
- UVA 10382 贪心(区间覆盖)
- UVA 10020 - Minimal coverage (贪心) 区间覆盖问题
- UVA 10020 简单贪心区间覆盖,各种WA,记录一下
- UVA 10382 Watering Grass (区间覆盖,贪心)
- UVa 10020 - Minimal coverage(区间覆盖并贪心)