题目描述
暑假到了,小明终于可以开心的看电视了。但是小明喜欢的节目太多了,他希望尽量多的看到完整的节目。
现在他把他喜欢的电视节目的转播时间表给你,你能帮他合理安排吗?
输入
输入包含多组测试数据。每组输入的第一行是一个整数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<iostream>
#include<algorithm>
using namespace std;
struct Interval
{
int si;
int ei;
};
bool cmp(Interval a,Interval b)
{
if (a.si!=b.si)return a.si>b.si;//先按开始的时间从晚到早排序
else return a.ei<b.ei;//如果开始时间相等,就按结束时间从早到晚排序
}
int main()
{
Interval programme[100];
int n;
//注意这个循环条件,当n=0,输入结束
while (scanf("%d",&n)!=EOF&&n!=0)
{
for (int i=0; i<n; i++)
{
cin>>programme[i].si>>programme[i].ei;
}//时间点按从到晚到早排序
sort(programme,programme+n,cmp);
int ans=1,lastSI = programme[0].si;
for (int i=1; i<n; i++)
{
if (programme[i].ei<=lastSI)
{
ans++;//中间无重合点加一
lastSI = programme[i].si;//以programme[i]作为新的选区
}
}
cout<<ans<<endl;
}
return 0;
}