UVA 10382 - Watering Grass 贪心

来源:互联网 发布:漫画制作软件 编辑:程序博客网 时间:2024/06/02 01:05

看题传送门 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1323


题目大意:有一块草坪长为L宽为w,在其中心线的不同位置出装有n个点状的喷泉装置。每个喷泉装置i可以将以它为中心,半径为Ri的圆形区域润湿。求可以把整个草坪润湿的最少的喷水装置。


好多天以前A的题目

贪心。

做法是把喷泉装置的圆形区域转化为矩形区域。按每个能喷到的最左边的坐标进行排序。每次选择喷泉的时候务必保证喷泉左边能覆盖到上一个的右边,并且尽量靠右。

注意浮点数比较大小的方法。


#include<cstdio>#include<algorithm>#include<iostream>#include<cmath>using namespace std;const int MAXN=10000+10;struct sprinkler{double l,r;bool operator <(const sprinkler &x)const{return l<x.l;}}sp[MAXN];int main(){int n,len,w;while(scanf("%d%d%d",&n,&len,&w)!=EOF){int cnt=0,x;double r,temp,h;h=w/2.0;for(int i=0;i<n;i++){scanf("%d%lf",&x,&r);if(r*2 <= w)//一开始没等于狂wa!!!!!continue;temp=sqrt(r*r-h*h);sp[cnt].l=x-temp;sp[cnt].r=x+temp;cnt++;}sort(sp,sp+cnt);//按最左边的距离排序double L=0,R=len,max_r;int ans=0;while(L<R){max_r=0;for(int i=0;i<cnt;i++)if( sp[i].l-L <1e-16 && max_r - sp[i].r <1e-16 )max_r=sp[i].r;if(fabs(max_r-L)<1e-16){     ans=-1; break;    }ans++;L=max_r;}printf("%d\n",ans);}}


原创粉丝点击