题目:
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
    HintINPUT DETAILS:

FJ needs to use planks of length 3 to cover 3 mud pools. The mud pools cover regions 1 to 6, 8 to 12, and 13 to 17.

OUTPUT DETAILS:

FJ can cover the mud pools with five planks of length 3 in the following way:
111222…333444555…

               .MMMMM..MMMM.MMMM....012345678901234567890

题义:
农民约翰想用几块等长的木板遮住他的农场,输入遮住距离有几段和木板的长度,和每段被遮住位置的起止位置,求出所用木板最少的数量。
思路:
定义一个结构体用来每一段的起止位置,定义一个函数,让每段根据起始时间从小到大排序,定义两个指针,记录每段的起止位置。在循环中计算每一段需要木板的数量,当每段距离的长度不恰好等于木板的倍数时,数量+1,更新末位置的指针,再与下一段的初位置进行比较,若大于下一段的初位置,则更新初位置的指针。
代码:

#include<iostream>
#include <stdio.h>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <algorithm>
#define exp 1e-6
typedef long long ll;
using namespace std;
struct node
{int begin;int end;
} a[10010];
bool cmp(node a,node b)
{return a.begin<b.begin;}
int main()
{int n,L,i,j;ios::sync_with_stdio(false);cin>>n>>L;for(i=0; i<n; i++)cin>>a[i].begin>>a[i].end;sort(a,a+n,cmp);int sum=0;int l=-1;for(i=0; i<n; i++){if(l>=a[i].end)continue;if(l>a[i].begin){int length=a[i].end-l;int num=length/L;if(length%L==0)num=num;if(length%L!=0)num++;l+=num*L;sum+=num;}else{int length=a[i].end-a[i].begin;int num=(length+L-1)/L;l=a[i].begin+num*L;sum+=num;}}cout<<sum<<endl;return 0;
}

贪心算法解题报告(D-Farmer John)相关推荐

  1. 贪心算法解题报告(区间覆盖问题)

    题目: Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the ot ...

  2. DAY1 贪心算法学习报告

    集训DAY1:贪心算法 学习报告 这天的题还有一道未解决,暂时不会代码实现,由于时间有限(精力是相对无限的),所以留待明天补档. /(课堂笔记) 贪心算法的核心:局部最优得整体最优 证明:数学归纳 微 ...

  3. prim算法_贪心算法详解(附例题)

    贪心算法的特征规律 贪心算法,"贪心"二字顾名思义,因此其规律特征就是更加注重当前的状态,贪心法做出的选择是对于当前所处状态的最优选择,它的解决问题的视角是微观的"局部& ...

  4. “365算法每日学计划”:03打卡-贪心算法

    自从开始做公众号开始,就一直在思考,怎么把算法的训练做好,因为思海同学在算法这方面的掌握确实还不够.因此,我现在想做一个"365算法每日学计划". "计划"的主 ...

  5. 贪心算法-01硬币找零问题

    硬币找零问题 前言 所谓贪心算法,就是遵循某种既定原则,不断选取当前条件下最优的选择来构造每一个子步骤的解决方案,直到获得问题最终的求解.即在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不 ...

  6. 贪心算法,递归算法,动态规划算法比较与总结

    一般实际生活中我们遇到的算法分为四类: 一>判定性问题        二>最优化问题        三>构造性问题        四>计算性问题 而今天所要总结的算法就是着重解 ...

  7. java每日打卡_“365算法每日学计划”:03打卡-贪心算法

    自从开始做公众号开始,就一直在思考,怎么把算法的训练做好,因为思海同学在算法这方面的掌握确实还不够.因此,我现在想做一个"365算法每日学计划"."计划"的主要 ...

  8. 最多可以参加的会议数目--贪心算法

    LeetCode 最多可以参加的会议数目 给你一个数组 event ,其中 events[i] = [startDayi, endDayi] ,表示会议 i 开始于 startDayi ,结束于 en ...

  9. 贪心算法及几个经典例子

    贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在 当前看来是最好的选择 .也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的 局部最优解 . 贪心算法没有固定的算法框 ...

最新文章

  1. 打好网约车“安全牌”,T3出行以人、车、路保障
  2. 机器学习笔记GBDT(一):原理
  3. ELK报错hese locations are not writable or multiple nodes were started without inc
  4. 阿里云正式推出高防DDoS专家服务
  5. 新Astaroth***病毒可利用杀毒软件窃取数据
  6. Django从理论到实战(part27)--ORM模型的增删改查操作
  7. 使用JGroups进行ElasticMQ消息复制
  8. linux 多个java_linux 同时出现两个java进程,新手~ 请详细说明,这个是怎么回事。 我就装了一个jdk...
  9. linux 管道 top,linux IPC总结——管道
  10. golang的一款cms内容管理系统
  11. Dev控件GridControl 的使用
  12. 六大方法来对付网站出现的负面信息
  13. mysql5.5java安装_配置非安装版的mysql 5.5
  14. Linux内核使用的字符串转整形数和16进制数
  15. 《深入浅出统计学》之统计学知识小结
  16. 第三方支付相关知识结构
  17. 解决loadrunner使用谷歌浏览器录制时打不开网页的问题
  18. 面向对象程序设计实验——编写Rational类
  19. 计算机网络术语sonet,计算机网络(第七版)谢希仁-第2章 物理层(示例代码)
  20. 实习笔记 —— MyBatis III (输入/输出映射)

热门文章

  1. 计算机知识大赛策划案,计算机基础知识大赛策划书新版
  2. 好一座假山!———初谈岳不群
  3. Docker容器下mysql数据库权限Access denied for user ‘‘@‘172.17.0.1‘ (using password: YES)
  4. 如何扎实的学好ABAP?我的个人经验
  5. [转]CSS编码规范
  6. C++ new失败处理
  7. 北京航空航天大学计算机科学与工程系,北京航空航天大学计算机科学与工程系.ppt...
  8. 使用python(matplotlib)打开图片
  9. 微盟股价逆变的背后: 经济寒冬已至,SaaS产业如何破局?
  10. 3.1_28_4 JDK8新特性【Date】之ZoneId 时区信息