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;
    }
  • 相关阅读:
    iOS之内存管理(ARC)
    分布式锁1 Java常用技术方案
    谈谈如何使用Netty开发实现高性能的RPC服务器
    前后端分离开发模式下后端质量的保证 —— 单元测试
    jquery实现"跳到底部","回到顶部"效果
    html内容超出了div的宽度如何换行让内容自动换行
    采用easyui+ajax+htm+ashx编写 通过用户对应角色 角色对应菜单 控制用户的访问权限
    javascript [] 与 {} 的区别
    图说设计模式
    T4教程1 T4模版引擎之基础入门
  • 原文地址:https://www.cnblogs.com/ahu-shu/p/3561704.html
Copyright © 2011-2022 走看看