首页 > 代码库 > bzoj 1986
bzoj 1986
1986: [USACO2004 Dec] Dividing the Path 划区灌溉
Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 145 Solved: 89
[Submit][Status]
Description
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.
Input
* 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.
Output
* Line 1: The minimum number of sprinklers required. If it is not possible to design a sprinkler head configuration for Farmer John, output -1.
Sample Input
1 2
6 7
3 6
Sample Output
HINT
Source
Gold
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<string>#include<algorithm>#include<queue>#include<vector>#include<set>using namespace std;deque<int> q;#define N 1000010int n,l,A,B,biao[N],f[N];bool flag;struct line{ int x,y;}e[N];int main(){ flag=true; scanf("%d%d",&n,&l); scanf("%d%d",&A,&B); for(int i=1;i<=n;i++) { scanf("%d%d",&e[i].x,&e[i].y); if(e[i].y-e[i].x>2*B) flag=false; } for(int i=1;i<=n;i++) for(int j=e[i].x+1;j<e[i].y;j++) biao[j]=0; if(flag) { f[0]=0; for(int i=1;i<=l;i++) { f[i]=(1<<30); if(i<2*A) continue; while(!q.empty()&&f[q.back()]>f[i-2*A]) q.pop_back(); q.push_back(i-2*A); while(!q.empty()&&q.front()<i-2*B) { q.pop_front(); } if(!biao[i]&&(i%2==0)) { f[i]=min(f[i],f[q.front()]+1); } } } if(!flag||f[l]==(1<<30)) printf("-1\n"); else printf("%d\n",f[l]); return 0;}
bzoj 1986