zoukankan      html  css  js  c++  java
  • 【贪心】【POJ-2437】Muddy roads

    Description

    Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainstorms and now contains (1 <= N <= 10,000) mud pools. 

    Farmer John has a collection of wooden planks of length L that he can use to bridge these mud pools. He can overlap planks and the ends do not need to be anchored on the ground. However, he must cover each pool completely. 

    Given the mud pools, help FJ figure out the minimum number of planks he needs in order to completely cover all the mud pools.

    Input

    * Line 1: Two space-separated integers: N and L 

    * Lines 2..N+1: Line i+1 contains two space-separated integers: s_i and e_i (0 <= s_i < e_i <= 1,000,000,000) that specify the start and end points of a mud pool along the road. The mud pools will not overlap. These numbers specify points, so a mud pool from 35 to 39 can be covered by a single board of length 4. Mud pools at (3,6) and (6,9) are not considered to overlap. 

    Output

    * Line 1: The miminum number of planks FJ needs to use.

    Sample Input

    3 3
    1 6
    13 17
    8 12
    

    Sample Output

    5

    /***********************************************************************************************************************
    题意:有n滩泥 木板长度为l 求最少需要多少木板才能覆盖这些泥
    思路:把泥升序排序,分三种情况讨论
    1、前一个木板完全覆盖了当前的泥 跳过
    2、前一个木板覆盖了一部分 则计算铺完剩下的泥需要多少木板
    3、前一个木板完全没接触到当前的木板 则更新端点
    ***********************************************************************************************************************/
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <vector>
    using namespace std;
    typedef struct 
    {
        int s, e;
    }node;
    node a[10000+10];
    bool cmp(node a, node b)
    {
        return a.s < b.s;
    }
    int main()
    {
        //freopen("data.in" , "r" , stdin);
        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 len = a[i].e - last;
                int num = (len + l - 1) / l;
                last += num * l;
                ans += num;
            }
            else
            {
                int len = a[i].e - a[i].s;
                int num = (len + l - 1) / l;
                last = a[i].s + num * l;
                ans += num;
            }
        }
        printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    vue 移动端商城搭建 (一)
    关于mantisBT2.22安装插件Inline column configuration 2.0.0时提示缺少依赖jQuery UI Library 1.8
    mantis2.22.1中添加管理员密码修改框
    mantisbt2.22.1 中使用自带的phpmailer发送邮件(实测可用)
    Mac配置Jenkins(构建Allure模板报告)
    ATX插件机制-学习学习
    查看包名和Activity的小工具
    pytest跳过指定的测试或模块
    pytest常用命令参数
    关于pytest使用allure生成报告时,报一堆警告和缺少XX模块
  • 原文地址:https://www.cnblogs.com/ahu-shu/p/3561704.html
Copyright © 2011-2022 走看看