码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-2037-今年暑假不AC

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

标签:http   io   ar   for   sp   代码   amp   ef   时间   

题目连接

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   io   ar   for   sp   代码   amp   ef   时间   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!