标签:des style blog http color io os java ar
http://acm.hdu.edu.cn/showproblem.php?pid=2037
贪心问题---选择不相交区间问题。小白书152页。
(ai,bi),b从小到大排序后,贪心策略:一定要选第一个区间。为什么小白书上有。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27976 Accepted Submission(s): 14837
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node { int s,e; }a[200]; bool cmp(node a,node b) { return a.e<b.e; } int main() { int n,i,ans; while(~scanf("%d",&n)&&n) { for(i=0;i<n;i++) scanf("%d%d",&a[i].s,&a[i].e); sort(a,a+n,cmp); ans=1; int f=a[0].e; for(i=1;i<n;i++) { if(a[i].s>=f) { ans++; f=a[i].e; } } printf("%d\n",ans); } return 0; }
标签:des style blog http color io os java ar
原文地址:http://www.cnblogs.com/cancangood/p/3979140.html