• 【贪心】【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;
    }
  • 相关阅读:
    文件读写
    使用HttpClient实现文件的上传下载
    TreeMap
    Linux的目录结构与文件权限
    Hibernate中get()和load()方法的区别
    Hibernate中openSession()与getCurrentSession()的区别与联系
    Hibernate核心类和接口
    Hibernate连接数据库
    Struts2中OGNL表达式的用法
    Struts2中Result的配置
  • 原文地址:https://www.cnblogs.com/ahu-shu/p/3561704.html
Copyright © 2020-2023  润新知