首页 > 其他 > 详细

HDU-2037-今年暑假不AC

时间:2014-11-01 21:44:17      阅读:286      评论:0      收藏:0      [点我收藏+]

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2037

贪心类题目

贪心法:就是遵循某种规则,不断贪心的选取当前最优策略的算法的设计方法。

此题解题思路 就是 在可选的节目中,每次选取结束时间最早的节目,以留更多的时间看其他节目,因为越早完成,就留的时间越多,节目可选越多。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct node
{
int s,e;
};

bool cmp(node a ,node b)
{
if(a.e!=b.e)
return a.e<b.e;
else
return a.s>b.s;
}

int main(void)
{
node t[110];
int n,i,j,k;
int ans;
while(scanf("%d",&n)==1&&n)
{
ans=1;
for(i=0;i<n;i++)
{
scanf("%d%d",&t[i].s,&t[i].e);
}
sort(t,t+n,cmp);
k=0;
for(i=1;i<n;i++)
{
if(t[i].s>=t[k].e)
{
ans++;
k=i;
}
}
printf("%d\n",ans);
}
return 0;
}

HDU-2037-今年暑假不AC

原文:http://www.cnblogs.com/liudehao/p/4067732.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!