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 的左端点被覆盖一次,然后剩下的继续这样考虑。。。
代码:
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 }