欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

HDU-2037-今年暑假不AC

发布时间:2025/4/9 编程问答 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 HDU-2037-今年暑假不AC 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目连接

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;
}

转载于:https://www.cnblogs.com/liudehao/p/4067732.html

总结

以上是生活随笔为你收集整理的HDU-2037-今年暑假不AC的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。