算法笔记 4.4 贪心 问题 A: 看电视

版权声明:版权归原作者CangyeChen所有,未经原作者允许不得转载本文内容,否则将视为侵权,转载或者引用本文内容请注明来源及原作者,对于不遵守此声明或者其他违法使用本文内容者,本人依法保留追究权等 https://blog.csdn.net/CANGYE0504/article/details/89295647

问题 A: 看电视

题目描述

暑假到了,小明终于可以开心的看电视了。但是小明喜欢的节目太多了,他希望尽量多的看到完整的节目。
现在他把他喜欢的电视节目的转播时间表给你,你能帮他合理安排吗?

输入

输入包含多组测试数据。每组输入的第一行是一个整数n(n<=100),表示小明喜欢的节目的总数。
接下来n行,每行输入两个整数si和ei(1<=i<=n),表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。
当n=0时,输入结束。

输出

对于每组输入,输出能完整看到的电视节目的个数。

样例输入

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

样例输出

5
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 101;

struct jm{
	int x;
	int y;
}I[maxn];

bool cmp(jm a,jm b)
{
	if(a.x!=b.x)
		return a.x>b.x;
	return a.y<b.y;
} 
	 
int main()
{
//	freopen("C://坚果云//算法//看电视in.txt","r",stdin);
//	freopen("C://坚果云//算法//看电视out.txt","w",stdout);

	int n,t;
	while(scanf("%d",&n)==1){
	if(!n) break;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&I[i].x,&I[i].y); 
	}
	sort(I,I+n,cmp);
	int num=1,temp=I[0].x;
	for(int j=0;j<n;j++)
	{
		if(I[j].y<=temp)
		{
			temp = I[j].x;
			num++;
		}	
	}
	printf("%d\n",num);
}
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/CANGYE0504/article/details/89295647