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.
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
1 6
13 17
8 12