Hdu2037 - 今年暑假不AC - 贪心算法

来源:互联网 发布:淘宝葡萄酒 编辑:程序博客网 时间:2024/05/17 05:15
#include<stdio.h>#include<algorithm>using namespace std;struct pro//记录节目开始和结束时间{int st,ed;}pr[105];int cmp(pro a,pro b)//将节目按照结束时间从小到大排序{return a.ed<b.ed;}int main(){int n,i,time,count;while(scanf("%d",&n)!=EOF){if(n==0){break;}for(i=1;i<=n;i++){scanf("%d%d",&pr[i].st,&pr[i].ed);}sort(pr+1,pr+n+1,cmp);//将节目按照结束时间从小到大排序time=0;//当前遍历到的节目结束的时间count=0;//节目数for(i=1;i<=n;i++){if(pr[i].st>=time)//如果下一节目与前一部分不交叉{time=pr[i].ed;//当前节目结束时间更新count++;//节目数增加一}}printf("%d\n",count);}}

0 0
原创粉丝点击