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;
    }
  • 相关阅读:
    设计模式(一)工厂模式Factory(创建型)
    Caused by: org.springframework.beans.factory.BeanCreationException
    从Delphi 7升级到Delphi XE
    system strategies of Resources Deadlock
    Service Manager流程,派BC_REPLY,唤醒FregServer流程,返回BR_TRANSACTION_COMPLETE,睡眠等待proc-&gt;wait
    Sqlserver2000联系Oracle11G数据库进行实时数据的同步
    火柴移动面试题
    Eclipse4.4设备egit插件提交本地项目代码到远程仓库
    V离MWare至Openstack至FDIO
    【BZOJ1791】【IOI2008】【基环树】island(status第一速度)
  • 原文地址:https://www.cnblogs.com/ahu-shu/p/3561704.html
Copyright © 2011-2022 走看看