Problem E

题意:看电视,计算出最多看多少个电视,已给出电视起始终止时间;
解体思路:思路这个题拿到手没多想,上课的例题,就照葫芦画瓢写了一个;
感悟:虽然刚开始学贪心,第一遍代码就AC了有点小小的成就感;
代码(G++ 0MS)
#include
#include
#include
using namespace std;
struct Ti{
    int s;
    int e;
    bool operator < (const Ti& other) const
    {
        if(this->e==other.e)
            this->s
        return this->e
    }
};
int main()
{
    //freopen("in.txt", "r", stdin);
    int n,s,e,ans=1,d=1;
    Ti t1[101],t2[101];
    while(~scanf("%d",&n)&&n)
    {
        ans=1;
        d=1;
        for(int i=0;i
        {
            scanf("%d%d",&s,&e);
            t1[i].s=s;
            t1[i].e=e;
        }
        stable_sort(&t1[0],&t1[n]);//按照节目时间由短到长排列
        t2[0]=t1[0];//因为第一个是必须看的;
        //cout<<"t1[0].s="<<t1[0].s<<" "<<"t1[0].e="<<t1[0].e<<endl;
        for(int i=1;i
        {
            if(t1[i].s>=t2[d-1].e)
            {
                //cout<<"t2[d-1].s="<<t2[d-1].s<<" "<<"t2[d-1].e="<<t2[d-1].e<<endl;
                t2[d++]=t1[i];
                ans++;
            }
            //cout<<"t1[i].s="<<t1[i].s<<" "<<"t1[i].e="<<t1[i].e<<endl;
        }
        printf("%d\n",ans);
    }
}
posted @ 2016-03-13 16:25  勿忘初心0924  阅读(107)  评论(0编辑  收藏  举报