HDOJ 2037 今年暑假不AC

来源:互联网 发布:西康路855半梦会所 js 编辑:程序博客网 时间:2024/05/21 22:56

 我的第一道贪心题..题目很好,但是今年暑假不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;}