给定一条草坪。草坪上有n个喷水装置。草坪长l米宽w米。。n个装置都有每个装置的位置和喷水半径。。要求出最少需要几个喷水装置才能喷满草坪。。喷水装置都是装在草坪中间一条水平线上的。
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.
What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?
Input
Input consists of a number of cases. The first line for each case contains integer numbers n, l and w with n <= 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)
Output
For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output -1.
1)如果圆的半径r*2<=草坪的宽度w,直接忽略该圆。
(2)对所有要考虑的圆按该圆与草坪上边界的的左交点的x坐标从小到大排序。
(3)如果所有圆能覆盖整个草坪,等价于圆与草坪的上边界交点所形成的区间覆盖了整个长度l。
(4)排完序后的圆,如果第一个的左交点x大于0,则无解。
(5)贪心选择圆的左交点不大于某个参考值,而右交点却最大的那个。(这里的参考值,一开始为0,如果找到了右交点最大的那个圆,就更新它)另外,如果找不到这样的圆,则无解。
(6)这里,圆的方程一般为(x-x0)^2+y^2=r^2,其中y=w/2.0,故不难得出,x=x0±sqrt(r^2-w^2/4.0)。
#include
#include
#include
#include
#define inf 0x3f3f3f3f
using namespace std;
struct M
{
double l,r;
}a[10020];
bool cmp(M a,M b)
{
if(a.l!=b.l) return a.l
}
int main()
{
std::ios::sync_with_stdio(false);
int n;
double len,w;
double c,rr;
while(cin>>n>>len>>w)
{
int num=0;
double maxr=-1.0;
for(int i=0;i
if(rr*2<=w)
continue;
else
{
double l=c-sqrt(rr*rr-w*w/4);
double r=c+sqrt(rr*rr-w*w/4);
if(r>=maxr)
{
maxr=max(maxr,r);
}
a[num].l=l;
a[num++].r=r;
}
}
sort(a,a+num,cmp);
int k=0;
if(a[0].l>0||maxr
maxx=a[i].r;
}
if(uu==maxx&&uu<len)
{
ww=1;
break;
}
ans++;
}
if(ww==0) cout<<ans<<endl;
else cout<<-1<<endl;
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章