http://www.lydsy.com/JudgeOnline/problem.php?id=1689

一开始我也想到了贪心,,,策略是如果两个连续的水池的距离小于l的话,那么就将他们链接起来,,,然后全部操作完后直接在每个大联通块上除以长度然后取木板就行了。。

但是不知道哪里错了T_T

正解:放木板尽量往后放。证明:假设当前i有水池,i-1没有水池,因为长度是固定的,那么从i放显然由于从i-1放木板。

然后模拟放木板即可。

优化:算出每个连续的水池需要放的数量,然后加上即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=10005;
struct dat { int x, y; }a[N];
bool cmp(const dat &a, const dat &b) { return a.x<b.x; }
int n, l; int main() {
read(n); read(l);
for1(i, 1, n) read(a[i].x), read(a[i].y);
sort(a+1, a+1+n, cmp);
int ans=0, now=0;
for1(i, 1, n) {
if(now>=a[i].y) continue;
now=max(now, a[i].x);
int t=a[i].y-now, t2=t/l;
if(t%l==0) ans+=t2, now+=t2*l;
else ans+=t2+1, now+=(t2+1)*l;
}
print(ans);
return 0;
}

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.

    牧场里下了一场暴雨,泥泞道路上出现了许多水坑,约翰想用一批长度 为L的木板将这些水坑盖住.    牧场里的道路可以看成一根数轴,每个水坑可以用数轴上的两个坐标表示,如(3,6)表示从3到6有一个长度为3的水 坑.所有的水坑都是不重叠的,(3,6)和(6,9)可以出现在同一个输入数据中,因为它们是两个连续的水坑,但不重叠.
    请你帮助约翰计算最少要用多少块木板才能将所有水坑盖住

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.

    第1行有二个用空格隔开的整数N和L.其中1≤N≤10000,表示水坑总数.L为木板长度.
接下来的N行每行有二个用整数si和ei(0≤si<ei≤109),表示一个水坑的两个坐标.

Output

* Line 1: The miminum number of planks FJ needs to use.

 
    一个整数,表示约翰盖住所有水坑最少要用多少块长为L的木板.

Sample Input

3 3
1 6
13 17
8 12

Sample Output

5

HINT

Source

【BZOJ】1689: [Usaco2005 Open] Muddy roads 泥泞的路(贪心)的更多相关文章

  1. bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路

    1689: [Usaco2005 Open] Muddy roads 泥泞的路 Description Farmer John has a problem: the dirt road from hi ...

  2. bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路【贪心】

    按左端点排序,贪心的选即可 #include<iostream> #include<cstdio> #include<algorithm> using namesp ...

  3. bzoj1689 / P1589 [Usaco2005 Open] Muddy roads 泥泞的路

    P1589 [Usaco2005 Open] Muddy roads 泥泞的路 简单的模拟题. 给水坑排个序,蓝后贪心放板子. 注意边界细节. #include<iostream> #in ...

  4. bzoj1689 [Usaco2005 Open] Muddy roads 泥泞的路

    Description Farmer John has a problem: the dirt road from his farm to town has suffered in the recen ...

  5. P1689: [Usaco2005 Open] Muddy roads 泥泞的路

    水题,模拟就行了,别忘了L>=r的时候直接更新下一个的L然后continue type node=record l,r:longint; end; var n,l,i,ans:longint; ...

  6. bzoj 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 最小点覆盖

    链接 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 思路 这就是个上一篇的稍微麻烦版(是变脸版,其实没麻烦) 用边长为1的模板覆盖地图上的没有长草的土地,不能覆盖草地 ...

  7. BZOJ 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场

    Description 大雨侵袭了奶牛们的牧场.牧场是一个R * C的矩形,其中1≤R,C≤50.大雨将没有长草的土地弄得泥泞不堪,可是小心的奶牛们不想在吃草的时候弄脏她们的蹄子.  为了防止她们的蹄 ...

  8. BZOJ1689: [Usaco2005 Open] Muddy roads

    1689: [Usaco2005 Open] Muddy roads Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 147  Solved: 107[Su ...

  9. [Usaco2005 Jan]Muddy Fields泥泞的牧场

    Description 雨连续不断的击打了放牛的牧场,一个R行C列的格子(1<=R<=50,1<=C<=50).虽然这对草来说是件好事,但这却使得一些没有草遮盖的土地变得很泥泞 ...

随机推荐

  1. Xcode 生成 ipa包

    原地址:http://zengwu3915.blog.163.com/blog/static/2783489720136213239916/ app store的审核收费的需要二周,免费的需要一个月左 ...

  2. android KK版本号收到短信后,点亮屏的操作

    alps/packages/apps/mms/src/comandroid\mms\transation\MessagingNotification.java private static void ...

  3. 算法笔记_028:字符串转换成整数(Java)

    1 问题描述 输入一个由数字组成的字符串,请把它转换成整数并输出.例如,输入字符串“123”,输出整数123. 请写出一个函数实现该功能,不能使用库函数. 2 解决方案 解答本问题的基本思路:从左至右 ...

  4. cocos2d-x CCScrollView 源代码分析

    版本号源代码来自2.x,转载请注明 另我实现了能够循环的版本号http://blog.csdn.net/u011225840/article/details/31354703 1.继承树结构 能够看出 ...

  5. BOM*创建工艺路线

    --工艺路线 DECLARE -- API input variables l_operation_tbl bom_rtg_pub.operation_tbl_type := bom_rtg_pub. ...

  6. C++求两个数的最大值

    //不使用if,:?等推断语句.求两个数字中最大的那个数字. #include<iostream> using namespace std; int main() { int a = -1 ...

  7. some issues that you should be take care of when use the plupload module

    1. the config arguments 'browse_button' should not be a single element like button etc. because in i ...

  8. spring in action 7.1 小结

    0 AbstractAnnotationConfigDispatcherServletInitializer剖析,在Servlet 3.0环境中,容器会在类路径中查找实现ServletContaine ...

  9. mysql 开启慢查询 如何打开mysql的慢查询日志记录

    mysql慢查询日志对于跟踪有问题的查询非常有用,可以分析出当前程序里有很耗费资源的sql语句,那如何打开mysql的慢查询日志记录呢,接下来将详细为您介绍 原文出自:http://www.jbxue ...

  10. Atitit.软件GUI按钮与仪表盘(01)--js区-----js格式化的使用

    Atitit.软件GUI按钮与仪表盘(01)--js区-----js格式化的使用 1. Chrome36( recomm) DEV TOOL>SOURCE> DSWEIHAMYAR  &q ...