UVA Watering Grass
贪心算法。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; struct Node { double lx; double rx; }node[10006]; bool cmp(Node a,Node b) { return a.lx<b.lx; } int main() { double n,m,w,l,r; while(scanf("%lf%lf%lf",&n,&m,&w)!=EOF) { int x=0,i,j,ans=0,ok=0; for(int i=0;i<n;i++) { scanf("%lf%lf",&l,&r); if(w/2>=r) continue; double t=(double)sqrt(r*r-((w*w)/4.0)); node[x].lx=l-t; node[x].rx=l+t; x++; } sort(node,node+x,cmp); double left=0.0,right=0.0; if(node[0].lx<=0) { i=0; while(i<x) { j=i; while(j<x && node[j].lx<=left) { if(node[j].rx>right) { right=node[j].rx; } j++; } if(i==j) break; i=j; ans++; left=right; if(left>=m) { ok=1; break; } } } if(ok)printf("%d\n",ans); else printf("-1\n"); } return 0; }