使用贪心算法安排电视节目收看,求最大。
它和以前学过的活动安排一样的。
#include <iostream>
#include<algorithm>
using namespace std;
struct show{
int startTime;
int endTime;
bool operator <(const show &a)const{
return endTime<a.endTime;
}
}t[1001];
int main()
{
int n;
while(cin>>n){
if(n==0)
break;
for(int i=0;i<n;i++){
cin>>t[i].startTime>>t[i].endTime;
}
sort(t,t+n);
int ans=0;
int currentTime=0;
for(int i=0;i<n;i++){
if(currentTime<=t[i].startTime){
currentTime=t[i].endTime;
ans++;
}
}
cout<<ans<<endl;
}
return 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
*/