HDU 3650 Hot Expo

本文介绍了一种基于贪心算法的活动安排问题解决方案。通过记录每个活动的开始和结束时间点,统计同一时间点上开始的活动数量,并在遇到活动结束时减少计数,从而找出所需的最小天数。

  贪心,以时间点i开始的活动数目为beg[i],结束的记为end[i], 然后每扫到一个开始和结

束,数组都计数。然后统计一天同一个时间点有多少个开始就行了,遇到结束要减去。最小

的天数就求出来了。

 

/*Accepted    3650    15MS    916K    881 B    C++    Yu*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

const int MAXN = 24 * 3600;
int beg[MAXN + 10], end[MAXN + 10];
int n, yMax, x, y, ans, cnt;

int main()
{
    int i;
    while(scanf("%d", &n), n)
    {
        memset(beg, 0, sizeof beg);
        memset(end, 0, sizeof end);
        ans = cnt = yMax = 0;
        for(i = 0; i < n; i ++)
        {
            scanf("%d%d", &x, &y);
            beg[x] ++; //这个时间开始的活动
            end[y] --; //这个时间结束的活动
            yMax = max(y, yMax);
        }
        for(i = 0; i <= yMax; i ++)
        {
            if(beg[i] > 0)
            {
                cnt += beg[i];
                ans = max(ans, cnt);
            }
            if(end[i] < 0)
            {
                cnt += end[i];
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/Yu2012/archive/2012/09/17/2688813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值