思路:贪心
为了使看的电视节目最多,应该看时间轴最先结束的电视节目,所以直接按照电视节目时间结束时间排序,创建一个集合,如果当前节目开始时间大于等与该集合最后一个节目的结束时间,说明该电视节目我们也可以观看。
# include <iostream>
# include <algorithm>
using namespace std;
typedef struct stu {
int x,y;
} A;
const int N=1000;
A a[N];
bool cmp(A x,A y) {
return x.y<y.y;
}
int main() {
int n;
while(cin>>n,n) {
for(int i=1; i<=n; i++) {
cin>>a[i].x>>a[i].y;
}
sort(a+1,a+n+1,cmp);
int res=0;
int cnt=0;
for(int i=1; i<=n; i++) {
if(a[i].x>=res) {
cnt++;
res=a[i].y;
}
}
cout<<cnt<<endl;
}
}