BZOJ1689: [Usaco2005 Open] Muddy roads

时间:2023-03-08 16:36:43

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

题解:
又是这样的题。。。
考虑边界,瞬间变水题。。。
第一个有泥泞的点一定需要作为 l 的左端点被覆盖一次,然后剩下的继续这样考虑。。。
代码:
 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int n,L;
struct ken{int l,r;} a[];
bool kp(const ken &i,const ken &j) {return i.l<j.l;}
void init()
{
scanf("%d%d",&n,&L);
int i;
for(i=;i<=n;i++)
scanf("%d%d",&a[i].l,&a[i].r);
sort(a+,a+n+,kp);
}
void work()
{
int i,len=,w=,ans=;
for(i=;i<=n;i++)
{if(w>=a[i].r) continue;
w=max(a[i].l,w);
while(w<a[i].r) {ans++; w+=L;}
}
printf("%d\n",ans);
}
int main()
{
init(); work();
return ;
}