Watering Grass UVA - 10382
题目链接
题意:有一个草坪长l米,宽w米,每个洒水器安在草坪的水平中心线上,题目给出数据n个喷水器,l,w,接下来给出每一个洒水器距中心线左端的距离以及其工作半径,求灌溉整个草条所需的最小洒水器数量。
思路:
首先,这道题有点坑,一直过不去,我就把所有数定义成double型,就过了。(听大佬说有精度误差,就算强制转换也不行。)
好气啊。。。。。
第一个点,给出的半径必须大于草坪半径,否则就continue,
第二点,要求出它的实际完全覆盖草坪的长度。
接下来就是一个普通的区间覆盖问题啦啦啦~
可以参考这位大佬的博客https://www.cnblogs.com/Draymonder/p/7215230.html
贴上我的AC代码
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int sum;
double n,l,w;
struct node
{
double a,b,c;
}s[10010];
bool cmp(node x,node y)
{
return x.a<y.a;
}
int solve()
{
int cnt=0;
double last=0,far=0;
for(int i=0;i<sum;i++)
{
if(last>=l)
return cnt;
if(s[i].a<=last)
far=max(far,s[i].b);
else if(s[i].a>last)
{
cnt++;
last=far;
if(s[i].a<=last)
far=max(far,s[i].b);
else
return -1;
}
}
if(last<l&&far>=l)return cnt+1;
if(far<l)return -1;
return cnt;
}
int main()
{
while(~scanf("%lf%lf%lf",&n,&l,&w))
{
int i,j;
sum=0;
double x,y;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&x,&y);
if(y<=w/2)
{
continue;
}
s[sum].a=x-sqrt(y*y-w/2*w/2);
//if(s[i].a<0)s[i].a=0;
s[sum++].b=x+sqrt(y*y-w/2*w/2);
//if(s[i].b>l)s[i].b=l;
//printf("****%f %f****\n",s[i].a,s[i].b);
}
sort(s,s+sum,cmp);
int ans=solve();
printf("%d\n",ans);
}
return 0;
}
共勉,加油