我是靠谱客的博主 酷炫鸵鸟,这篇文章主要介绍第一次寒假积分赛补题记录,现在分享给大家,希望可以做个参考。

今年暑假不AC HDU - 2037

题目:
在这里插入图片描述
这题是贪心算法,活动安排问题,可以使用结构体。
活动安排问题的贪心算法要先将各个活动结束的时间升序排序,然后再将第一个活动的结束时间与第二个活动的开始时间作比较…依此类推。
在这里插入图片描述
AC代码如下:

#include <iostream>
#include<algorithm>
using namespace std;
struct show
{
	int s;
	int e;
};
bool cmp(const show& a, const show& b)
{
	if (a.e < b.e)
		return true;
	else
		return false;
}
int main()
{
	show time[110];
	int n, b[110], cot;
	while (cin >> n)
	{
		if (n == 0)
			break;
		cot = 1;
		for (int i = 1; i <= n; i++)
		{
			cin >> time[i].s>> time[i].e;
		}
		sort(time + 1, time + 1 + n, cmp);
		b[1] = true;//排序后第一个一定可以
		int pre = 1;
		for (int j = 2; j <= n; j++)
		{
			if (time[j].s >= time[pre].e)
			{
				b[j] = true;
				pre = j;
				cot++;
			}
		}
		cout << cot << endl;
	}
	return 0;
}

最后

以上就是酷炫鸵鸟最近收集整理的关于第一次寒假积分赛补题记录的全部内容,更多相关第一次寒假积分赛补题记录内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(108)

评论列表共有 0 条评论

立即
投稿
返回
顶部