HDU-2037今年暑假不AC(经典贪心)

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2037

解析:

每个节目,有开始时间和结束时间。

考虑3个贪心策略:

1:节目持续时间最短

2:最早开始

3:最早结束

可以发现,对于第2种,如果某个节目最早开始,但是迟迟不结束,那么其他节目就没得看了。

第1种,随便举个例子,就可以否定掉。

越早结束,那么越能更早得看下一个节目。

所以在排序时,按结束时间从小到大排序。

开始时间的排序,怎么排都是无影响的。

#include<cstdio>
#include<stack>
#include<map>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=111+10;
struct node
{
    int l,r;
}st[maxn];
bool cmp(node a , node b)
{
    if(a.r==b.r)
        return a.l>b.l;//自便
    return a.r<b.r;
}
int main()
{    // 4 0 20
    int n;
    while(scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
            cin>>st[i].l>>st[i].r;
        sort(st+1,st+1+n,cmp);
        int ans=1;
        int ft=st[1].r;
        for(int i=2;i<=n;i++)
        {
            if(st[i].l>=ft)
            {
                ft=st[i].r;
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}

 

posted @ 2020-07-07 23:43  liyexin  阅读(149)  评论(0)    收藏  举报