分析:
给出N个圆的圆心坐标和圆半径,然后以其中一个圆心做一个大圆要求覆盖所以圆并使得覆盖面积大于被覆盖圆的1/2,求该大圆的最小半径。
因为N最多为20个,我们可以直接搜索每一个圆心,然后二分查半径范围,直到确定半径小于误差。题解:
1.建立圆:
const double eps = 1e-8;
const double PI = acos(-1.0);
struct point
{
double x;
double y;
};
struct circle
{
point c;
double r;
}Circle[22], tmp;
2.求两圆相交面积:
double Area(circle a,circle b)
{//两圆关系随意,都能求相交面积。
circle tmpa=a, tmpb=b;
if(tmpa.r < tmpb.r) swap(tmpa,tmpb);
double area = 0;
double dd = dis(tmpa.c, tmpb.c);
if(dd > tmpa.r -tmpb.r && dd < tmpa.r + tmpb.r){
double cos1 = (tmpa.r*tmpa.r+dd*dd-tmpb.r*tmpb.r)/(2*tmpa.r*dd);
double cos2 = (tmpb.r*tmpb.r+dd*dd-tmpa.r*tmpa.r)/(2*tmpb.r*dd);
double th1 = 2*acos(cos1);
double th2 = 2*acos(cos2);
double s1 = 0.5*tmpa.r*tmpa.r*sin(th1);
double s2 = 0.5*tmpb.r*tmpb.r*sin(th2);
double s3 = (th1/2)*tmpa.r*tmpa.r;
double s4 = (th2/2)*tmpb.r*tmpb.r;
area = s3+s4-s1-s2;
}
else if(dd <= tmpa.r - tmpb.r)
area = PI*tmpb.r*tmpb.r;
return area;
}
3.二分搜索:
const double eps = 1e-8;
const double PI = acos(-1.0);
bool check(circle a)
{
for(int i=0;i<N;i++)
{
if( Area(a, Circle[i])*2 < PI*Circle[i].r*Circle[i].r)
return false;
}
return true;
}
double bin_search(double l, double r, circle tmp)
{
double mid;
while(r-l>=eps)
{
mid=(l+r)/2.0;
tmp.r=mid;
if(check(tmp))
r=mid;
else
l=mid+eps;
}
return mid;
}
- AC代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
int N;
struct point
{
double x;
double y;
};
struct circle
{
point c;
double r;
}Circle[22], tmp;
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Area(circle a,circle b)
{
circle tmpa=a, tmpb=b;
if(tmpa.r<tmpb.r) swap(tmpa,tmpb);
double area = 0;
double dd = dis(tmpa.c, tmpb.c);
if(dd>tmpa.r-tmpb.r&&dd<tmpa.r+tmpb.r){
double cos1 = (tmpa.r*tmpa.r+dd*dd-tmpb.r*tmpb.r)/(2*tmpa.r*dd);
double cos2 = (tmpb.r*tmpb.r+dd*dd-tmpa.r*tmpa.r)/(2*tmpb.r*dd);
double th1 = 2*acos(cos1);
double th2 = 2*acos(cos2);
double s1 = 0.5*tmpa.r*tmpa.r*sin(th1);
double s2 = 0.5*tmpb.r*tmpb.r*sin(th2);
double s3 = (th1/2)*tmpa.r*tmpa.r;
double s4 = (th2/2)*tmpb.r*tmpb.r;
area = s3+s4-s1-s2;
}
else if(dd<=tmpa.r-tmpb.r)
area = PI*tmpb.r*tmpb.r;
return area;
}
bool check(circle a)
{
for(int i=0;i<N;i++)
{
if( Area(a, Circle[i])*2 < PI*Circle[i].r*Circle[i].r)
return false;
}
return true;
}
double bin_search(double l, double r, circle tmp)
{
double mid;
while(r-l>=eps)
{
//printf("%.4lf&&&%.4lf\n",l , r);
mid=(l+r)/2.0;
//printf("%.4lf\n",mid);
tmp.r=mid;
if(check(tmp))
r=mid;
else
l=mid+eps;
}
return mid;
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> N;
for(int i=0;i<N;i++)
{
scanf("%lf%lf%lf", &Circle[i].c.x, &Circle[i].c.y, &Circle[i].r);
}
double R = 1000000;
for(int i=0;i<N;i++)
{
tmp.c = Circle[i].c;
R = min(R, bin_search( 0, 30000, tmp) );
}
printf("%.4lf\n",R);
}
}