Open-air shopping malls 两圆面积相交,二分搜索半径

Open-air shopping malls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2078    Accepted Submission(s): 745


Problem Description
The city of M is a famous shopping city and its open-air shopping malls are extremely attractive. During the tourist seasons, thousands of people crowded into these shopping malls and enjoy the vary-different shopping.

Unfortunately, the climate has changed little by little and now rainy days seriously affected the operation of open-air shopping malls—it’s obvious that nobody will have a good mood when shopping in the rain. In order to change this situation, the manager of these open-air shopping malls would like to build a giant umbrella to solve this problem.

These shopping malls can be considered as different circles. It is guaranteed that these circles will not intersect with each other and no circles will be contained in another one. The giant umbrella is also a circle. Due to some technical reasons, the center of the umbrella must coincide with the center of a shopping mall. Furthermore, a fine survey shows that for any mall, covering half of its area is enough for people to seek shelter from the rain, so the task is to decide the minimum radius of the giant umbrella so that for every shopping mall, the umbrella can cover at least half area of the mall.
 

Input
The input consists of multiple test cases.
The first line of the input contains one integer T (1<=T<=10), which is the number of test cases.
For each test case, there is one integer N (1<=N<=20) in the first line, representing the number of shopping malls.
The following N lines each contain three integers X,Y,R, representing that the mall has a shape of a circle with radius R and its center is positioned at (X,Y). X and Y are in the range of [-10000,10000] and R is a positive integer less than 2000.
 

Output
For each test case, output one line contains a real number rounded to 4 decimal places, representing the minimum radius of the giant umbrella that meets the demands.
 

Sample Input
1 2 0 0 1 2 0 1
 

Sample Output
2.0822
分析:从给出的一些圆中选出合适的圆心,在半径最小的情况下 至少覆盖剩余圆的面积的一半

code:

<span style="font-size:18px;">#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

#define sqr(x) ((x)*(x))
const double eps=1e-9;
const double inf=1e10;
const double pi=acos(-1.0);
const int maxn=22;

struct point
{
    double x, y;
};
struct circle
{
    point o;
    double r;
} c[maxn];
int n;

double dis(point a, point b)
{
    return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
int sgn(double x)
{
    if(fabs(x)<eps)return 0;//==0
    if(x>0)return 1;
    return -1;//<0
}

double circle_inter_area(circle c1, circle c2)//两圆相交面积
{
    double d=dis(c1.o, c2.o);
    if(c1.r<c2.r)swap(c1, c2);

    if(sgn(d-c1.r-c2.r)>=0)  return 0;//相离或外切
    if(sgn(d-(c1.r-c2.r))<=0)return pi*sqr(c2.r);//内含或内切

    double ang1, ang2;
    ang1=acos((sqr(c1.r)+sqr(d)-sqr(c2.r))/(2.0*c1.r*d));
    ang2=acos((sqr(c2.r)+sqr(d)-sqr(c1.r))/(2.0*c2.r*d));
    double res=ang1*sqr(c1.r)+ang2*sqr(c2.r)-d*c1.r*sin(ang1);
    return res;
}
bool ok(double R)
{
    int i, j;
    circle C;
    for(i=0; i<n; i++)
    {
        C.o=c[i].o;//每一个圆的圆心不变 二分半径 将心构成的圆与剩下的圆进行相交圆面积测试 观察是否存在满足要求的半径
        C.r=R;
        for(j=0; j<n; j++)
            if(sgn(circle_inter_area(C, c[j])-pi*sqr(c[j].r)*0.5)<0)//两圆相交面积如果大于另一个圆的面积则是满足条件的
                break;
        if(j==n)return true;
    }
    return false;
}
double binary_solve()
{
    double l, r, mid, res;
    l=0;
    r=inf;
    while(l+eps<r)
    {
        mid=(l+r)/2;
        if(ok(mid))
        {
            r=mid-eps;
            res=mid;
        }
        else l=mid+eps;
    }
    return res;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i=0; i<n; i++)
            scanf("%lf%lf%lf", &c[i].o.x, &c[i].o.y, &c[i].r);

        printf("%.4lf\n", binary_solve());
    }
    return 0;
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值