我的第一道贪心题..题目很好,但是今年暑假不AC怎么行呢~~
题目是经典的活动安排问题,按照结束时间升序排序,尽量做结束的早的事情,以便留下更多的时间给剩下的事情(节目)
#include <cstdio>
#include <algorithm>
using namespace std;
struct tv{
int s,e;
}t[101];
bool cmp(tv a,tv b){
return (a.e-b.e)<0;
}
int main(){
int n;
while(scanf("%d",&n)&&n!=0){
for(int i=0;i<n;i++){
scanf("%d%d",&t[i].s,&t[i].e);
}
sort(t,t+n,cmp);//按照结束时间升序排序
int res=0,end=t[0].s;
for(int i=0;i<n;i++){
if(t[i].s>=end){
res++;
end=t[i].e;
}
}
printf("%d\n",res);
}
return 0;
}