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


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


题目: http://acm.hdu.edu.cn/showproblem.php?pid=2037


很经典的活动安排问题变形, 与算法教材的活动安排一样,依据结束时间进行排序,然后相容的累加就可以。


// 经典贪心问题  活动时间安排的简单变形
// 按活动结束时间,递增排序, 结束时间早的。优先选择

#include <bits/stdc++.h>

using namespace std;

typedef struct active
{
    int st;
    int ed;
    bool operator < (const struct active &at) const
    {
        return this->ed < at.ed;
    }
}Active;

const int MAX = 100+5;
Active arr[MAX];

int main(void)
{
    //freopen("in.txt", "r", stdin);

    int n = 0;
    while(cin>>n && n != 0)
    {
        for(int i=0; i<n; ++i)
            scanf("%d%d", &arr[i].st, &arr[i].ed);

        sort(arr, arr+n);
        //cout<<arr[0].ed<<endl;

        int cnt = 1;
        int j = 0;
        for(int i=1; i<n; ++i)
        {
            // 假设没有冲突
            if (arr[i].st >= arr[j].ed)
            {
                j = i;
                cnt++;
            }
        }

        printf("%d\n", cnt);
    }

    return 0;
}


posted @ 2017-06-26 12:39  lytwajue  阅读(147)  评论(0编辑  收藏  举报