题意:有一个长方形草坪,长为l,宽为w,在它的水平中心线上可以安装n个喷水器来为草坪浇水,每个喷水器能够灌溉的范围是以该喷水器为中心的半径为r的圆。我们要灌溉整个草坪,问你最少需要的喷水器数。
题解:很明显这是一个最小区间覆盖问题~~(老长时间不写了,写个blog记录下)~~
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
struct node{
double left;
double right;
}a[10009];
double solve(double r,double w)
{
double tmp=r*r-w*w/4;
return sqrt(tmp);
}
bool cmp(node x,node y)
{
return x.right>y.right;
}
int main()
{
int n,cnt,ans;
double x,r,l,w,en;
while(~scanf("%d%lf%lf",&n,&l,&w))
{
cnt=0;ans=0;en=0;
for(int i=1;i<=n;i++)
{
cin>>x>>r;
if(2*r<w) continue;
double tmp;
tmp=solve(r,w);
a[++cnt].left=x-tmp;
a[cnt].right=x+tmp;
}
sort(a+1,a+1+cnt,cmp);
while(en<l)
{
int i;
for(i=1;i<=cnt;i++)
{
if(a[i].left<=en&&a[i].right>en)
{
en=a[i].right;
ans++;
break;
}
}
if(i==cnt+1) break;
}
if(en<l)
cout<<"-1"<<endl;
else
cout<<ans<<endl;
}
return 0;
}