今年暑假不AC(水题)

来源:互联网 发布:一骑当千镜头数据 编辑:程序博客网 时间:2024/05/02 01:39

 

虽然这道题水的不行,但是虽然做了不少题,但是贪心一直是自己的一个BUG,今天稍微做了点,作为理解贪心的一个开始...

 

贪心的特点性质见博客http://blog.csdn.net/u014665013/article/details/40748115

 

打开题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2037

 

#include <iostream>#include <string>#include<algorithm>using namespace std;struct T{int start,end;}arr[110];bool compare(T a,T b){return a.end<b.end;   //升序排列,如果改为return a>b,则为降序}int main (){int n;while(scanf("%d",&n)&&n!=0){int i=0;int temp=n;while(n--){scanf("%d%d",&arr[i].start,&arr[i].end);i++;}sort(arr,arr+temp,compare);int count=1,mem=arr[0].end;for(int j=1;j<temp;j++)if(arr[j].start>=mem){count++,mem=arr[j].end;}printf("%d\n",count);}return 0;}


 

0 0
原创粉丝点击