电影节(贪心,结构体重载,sort)

电影节

总时间限制: 

1000ms

内存限制: 

65536kB

描述

大学生电影节在北大举办! 这天,在北大各地放了多部电影,给定每部电影的放映时间区间,区间重叠的电影不可能同时看(端点可以重合),问李雷最多可以看多少部电影。

输入

多组数据。每组数据开头是n(n<=100),表示共n场电影。
接下来n行,每行两个整数(0到1000之间),表示一场电影的放映区间
n=0则数据结束

输出

对每组数据输出最多能看几部电影

样例输入

8
3 4
0 7 
3 8 
15 19
15 20
10 15
8 18 
6 12 
0

样例输出

3

来源

#include<bits/stdc++.h>
using namespace std;
struct Film{
	int s;
	int end;
	bool operator<(const Film &f)const
	{
		return end<f.end;
	}
}f[106];
int main()
{
	int n;
    while(1)
	{
	 cin>>n;
	 if(n==0) break;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&f[i].s,&f[i].end);
	}
	sort(f,f+n);
	int count=1;
	int preend=f[0].end;
	for(int i=1;i<n;i++)
	{
		if(preend<=f[i].s)
		{
			count++;
			preend=f[i].end;
		}
		else
		 continue;
	}
	cout<<count<<endl;
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SSYITwin/article/details/81504703
今日推荐