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;
    }
  • 相关阅读:
    POJ 2456 Aggressive cows (二分)
    ACM北大暑期课培训第二天
    ACM北大暑期课培训第一天
    Rust Lang Book Ch.14 Crates.io
    Rust Lang Book Ch.13 Iterators, Closures
    Rust Lang Book Ch.12 An I/O Project: Building a Command Line Program
    Rust Lang Book Ch.11 Automated Tests
    Rust Lang Book Ch.10 Generic Types, Traits. and Lifetimes
    Rust Lang Book Ch.9 Error Handling
    HDU-2087-KMP-水题
  • 原文地址:https://www.cnblogs.com/ahu-shu/p/3561704.html
Copyright © 2011-2022 走看看