题目大意:给出N个节目的播放时间和结束时间,求最多能看多少节目?
解题思路:贪心求解,将输入的节目按照结束时间排序,每次遍历获取尽可能多的节目数,详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2037
code:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100+10;
int n,m;
struct node{
int s,e;
}p[MAXN];
int cmp(const node a,const node b){return a.e<b.e;}
int main(){
while(scanf("%d",&n)!=EOF && n){
for(int i=0;i<n;i++)
scanf("%d%d",&p[i].s,&p[i].e);
sort(p,p+n,cmp); //将结束点作为排序基点
int tmp=0;m=1;
for(int i=1;i<n;i++) //贪心求解,尽量获取最多的结束点
if(p[i].s>=p[tmp].e){
tmp=i;
m++;
}
printf("%d\n",m);
}
return 0;
}