BZOJ1689: [Usaco2005 Open] Muddy roads

1689: [Usaco2005 Open] Muddy roads

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 147  Solved: 107
[Submit][Status][Discuss]

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. 有N个MUD,用长度为L的木块去Cover.问至少要多少块.

Output

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

Sample Input

3 3
1 6
13 17
8 12

INPUT 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.

Sample Output

5

OUTPUT DETAILS:

FJ can cover the mud pools with five planks of length 3 in the
following way:
111222..333444555....
.MMMMM..MMMM.MMMM....
012345678901234567890

HINT

Source

Silver

题解:

又是这样的题。。。

考虑边界,瞬间变水题。。。

第一个有泥泞的点一定需要作为 l 的左端点被覆盖一次,然后剩下的继续这样考虑。。。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<iostream>
 7 using namespace std;
 8 int n,L;
 9 struct ken{int l,r;} a[10002];
10 bool kp(const ken &i,const ken &j) {return i.l<j.l;}
11 void init()
12 {
13     scanf("%d%d",&n,&L);
14     int i;
15     for(i=1;i<=n;i++)
16        scanf("%d%d",&a[i].l,&a[i].r);
17     sort(a+1,a+n+1,kp);
18 }
19 void work()
20 {
21     int i,len=0,w=0,ans=0;
22     for(i=1;i<=n;i++)
23        {if(w>=a[i].r) continue;
24         w=max(a[i].l,w);
25         while(w<a[i].r) {ans++; w+=L;}
26        }
27     printf("%d\n",ans);
28 }
29 int main()
30 {
31     init(); work();
32     return 0;
33 }

时间: 2024-10-01 02:19:33

BZOJ1689: [Usaco2005 Open] Muddy roads的相关文章

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路 题意: 数轴上n个互不覆盖的区间,问要用多少个长为L的线段覆盖.n≤10000 题解: 排序区间,然后从每个区间左端点开始铺木板,如果最后一块木板能够铺到下一个区间就铺,以此类推. 代码: 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #define maxn 10100 5 #define inc(i

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

P1589 [Usaco2005 Open] Muddy roads 泥泞的路 简单的模拟题. 给水坑排个序,蓝后贪心放板子. 注意边界细节. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struct data{int l,r;}a[10002]; 7 bool cmp(const data

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

1689: [Usaco2005 Open] 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

POJ2437 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

贪心/poj 2437 Muddy roads

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 struct node 6 { 7 int st,ed; 8 }; 9 node a[10010]; 10 11 int n,L,ans,pre; 12 13 bool cmp(node x,node y) 14 { 15 return x.st<y.st; 16 } 17 18 int mai

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

Description 大雨侵袭了奶牛们的牧场.牧场是一个R * C的矩形,其中1≤R,C≤50.大雨将没有长草的土地弄得泥泞不堪,可是小心的奶牛们不想在吃草的时候弄脏她们的蹄子.  为了防止她们的蹄子被弄脏,约翰决定在泥泞的牧场里放置一些木板.每一块木板的宽度为1个单位,长度任意.每一个板必须放置在平行于牧场的泥地里.    约翰想使用最少的木板覆盖所有的泥地.一个木板可以重叠在另一个木板上,但是不能放在草地上. Input 第1行:两个整数R和C. 第2到R+1行:每行C个字符,其中"*'代

bzoj1735 [Usaco2005 jan]Muddy Fields 泥泞的牧场

传送门 分析 我们知道对于没有障碍的情况就是将横轴点于纵轴点连边 于是对于这种有障碍的情况我们还是分横轴纵轴考虑 只不过对于有障碍的一整条分为若干个无障碍小段来处理 然后将标号小段连边,跑最大匹配即可 代码 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cctype> #includ

noip知识点总结之--贪心

一.什么是贪心 贪心算法嘛... 就是在对某个问题求解时,总是做出在当前看来是最好的选择 In other wors,并不是从整体最优上加以考虑,而是在获得某种意义上的局部最优解 二.贪心算法的适用前提 局部的最优解能导致最后整体的最优解,即局部的最优解不受该部分以外的东西的影响 对于贪心算法,我们需要证明:整个问题的最优解一定由在贪心策略中存在的子问题的最优解得来的 实际上,能用贪心算法的问题很少,大部分看上去能用贪心算法去做的题目,其实都得不到最优解T T(这时候就需要运用动态规划了) 而看

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734