POJ 2437 Muddy roads【贪心】(区间覆盖)

题目链接:https://vjudge.net/contest/194475#problem/C

 

题目大意:

有n滩泥 木板长度为L 求至少需要多少木板才能覆盖这些泥

 

解题思路:

把泥块的起点升序排序,分三种情况讨论
1、前一个木板完全覆盖了当前的泥 跳过
2、前一个木板覆盖了一部分 则计算铺完剩下的泥需要多少木板
3、前一个木板完全没接触到当前的木板 则更新端点

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAXN 10000+100
typedef struct
{
    int s, e;
}node;
node a[MAXN];
bool cmp(node a, node b)
{
    return a.s < b.s;
}
int main()
{
    int n, l;
    scanf("%d %d", &n, &l);
    for (int i = 0; i < n; i++)
        scanf("%d %d", &a[i].s, &a[i].e);
    sort(a, a + n, cmp);
    int ans = 0;
    int last = -1;
    for (int i = 0; i < n; i++)
    {
        if (last >= a[i].e)
            continue;
        if (last > a[i].s)
        {
            int num = ceil((a[i].e - last)*1.0 / l);      //向上取整函数
            last += num * l;
            ans += num;
        }
        else
        {
            int num = ceil((a[i].e - a[i].s)*1.0/l);
            last = a[i].s + num * l;
            ans += num;
        }
    }
    printf("%d\n", ans);
    return 0;
}
 
 

2018-04-21

转载于:https://www.cnblogs.com/00isok/p/8902344.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值