2373:Dividing the Path(动态规划)
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- Farmer John's cows have discovered that the clover growing along the ridge of the hill in his field is particularly good. To keep the clover watered, Farmer John is installing water sprinklers along the ridge of the hill.
To make installation easier, each sprinkler head must be installed along the ridge of the hill (which we can think of as a one-dimensional number line of length L (1 <= L <= 1,000,000); L is even).
Each sprinkler waters the ground along the ridge for some distance in both directions. Each spray radius is an integer in the range A..B (1 <= A <= B <= 1000). Farmer John needs to water the entire ridge in a manner that covers each location on the ridge by exactly one sprinkler head. Furthermore, FJ will not water past the end of the ridge in either direction.
Each of Farmer John's N (1 <= N <= 1000) cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval (S,E). Each of the cow's preferred ranges must be watered by a single sprinkler, which might or might not spray beyond the given range.
Find the minimum number of sprinklers required to water the entire ridge without overlap. - 输入
- * Line 1: Two space-separated integers: N and L
* Line 2: Two space-separated integers: A and B
* Lines 3..N+2: Each line contains two integers, S and E (0 <= S < E <= L) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge and so are in the range 0..L. - 输出
- * Line 1: The minimum number of sprinklers required. If it is not possible to design a sprinkler head configuration for Farmer John, output -1.
- 样例输入
-
2 8 1 2 6 7 3 6
- 样例输出
-
3
- 提示
- INPUT DETAILS:
Two cows along a ridge of length 8. Sprinkler heads are available in integer spray radii in the range 1..2 (i.e., 1 or 2). One cow likes the range 3-6, and the other likes the range 6-7.
OUTPUT DETAILS:
Three sprinklers are required: one at 1 with spray distance 1, and one at 4 with spray distance 2, and one at 7 with spray distance 1. The second sprinkler waters all the clover of the range like by the second cow (3-6). The last sprinkler waters all the clover of the range liked by the first cow (6-7). Here's a diagram:
|-----c2----|-c1| cows' preferred ranges
|---1---|-------2-------|---3---| sprinklers
+---+---+---+---+---+---+---+---+
0 1 2 3 4 5 6 7 8
The sprinklers are not considered to be overlapping at 2 and 6. - 来源
- USACO 2004 December Gold
- 郭炜老师讲解:https://2d.hep.com.cn/187723/9
-
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int INF=1<<30; 4 const int MAXL=1000010; 5 const int MAXN=1010; 6 int F[MAXL],cows[MAXL],cowVary[MAXL]; 7 int N,L,A,B; 8 struct Fx { 9 int x,F; 10 bool operator < (const Fx &a)const { 11 return F>a.F; 12 } 13 Fx(int xx=0,int ff=0):x(xx),F(ff) { 14 } 15 16 }; 17 priority_queue <Fx> q; 18 int main() { 19 cin>>N>>L>>A>>B; 20 A<<=1;B<<=1; 21 memset(cowVary,0,sizeof(cowVary)); 22 for(int i=0; i<N; i++) { 23 int s,e; 24 cin>>s>>e; 25 ++cowVary[s+1]; 26 --cowVary[e]; 27 } 28 int inCows=0; 29 for(int i=0; i<=L; i++) { 30 F[i]=INF; 31 inCows+=cowVary[i]; 32 cows[i]=inCows; 33 } 34 for(int i=A; i<=B; i+=2) { 35 if(!cows[i]) { 36 F[i]=1; 37 if(i<=B+2-A)q.push(Fx(i,1)); 38 } 39 } 40 for(int i=B+2; i<=L; i+=2) { 41 if(!cows[i]) { 42 Fx fx; 43 while(!q.empty()) { 44 fx=q.top(); 45 if(fx.x<i-B) { 46 q.pop(); 47 } else { 48 break; 49 } 50 51 } 52 if(!q.empty())F[i]=fx.F+1; 53 } 54 if(F[i-A+2]!=INF) { 55 q.push(Fx(i-A+2,F[i-A+2])); 56 } 57 } 58 if(F[L]==INF)cout<<-1<<endl; 59 else cout<<F[L]<<endl; 60 return 0; 61 }
越努力越幸运