Problem E

概述:输入若干个节目的起始时间,选出能完整观看的节目的最大数。

思路:贪心算法问题,解题的关键是在有限的时间内安排尽量多的节目。所以将节目按开始的时间排序,然后遍历所有节目,将前一个的结束时间与下一个节目开始时间冲突的节目排除,随后统计结果。

感想:第一个AC的题,非常简单。

#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
struct Info
{
	int start, end;
	bool flag;
};
bool cmp(const Info &a, const Info &b)
{
	return a.end < b.end;
}
int main()
{
	//ifstream cin("aaa.txt");
	int n;
	vector<Info>v;
	while (cin >> n&&n != 0)
	{
		v.clear();
		int sum = 1;
		Info a;
		while (n--)
		{
			cin >> a.start >> a.end;
			a.flag = 0;
			v.push_back(a);
		}
		stable_sort(v.begin(), v.end(),cmp);
		vector<Info>::iterator it1=v.begin();
		it1->flag = 1;
		for (vector<Info>::iterator it = v.begin()+1; it != v.end() ; ++it)
		{
			if (it1->flag && it->start >= it1->end)
			{
				it->flag = 1;
				it1 = it;
				++sum;
			}			
		}
		cout << sum << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值