HDU-6242:Geometry Problem(随机化+几何)

 

                                           Geometry Problem

                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
                                                         Total Submission(s): 1587    Accepted Submission(s): 288
                                                                                       Special Judge

 

Problem Description

Alice is interesting in computation geometry problem recently. She found a interesting problem and solved it easily. Now she will give this problem to you :

You are given N distinct points (Xi,Yi) on the two-dimensional plane. Your task is to find a point P and a real number R, such that for at least ⌈N2⌉ given points, their distance to point P is equal to R.

 

 

Input

The first line is the number of test cases.

For each test case, the first line contains one positive number N(1≤N≤10^5).

The following N lines describe the points. Each line contains two real numbers Xi and Yi (0≤|Xi|,|Yi|≤10^3) indicating one give point. It's guaranteed that N points are distinct.

 

 

Output

For each test case, output a single line with three real numbers XP,YP,R, where (XP,YP) is the coordinate of required point P. Three real numbers you output should satisfy 0≤|XP|,|YP|,R≤10^9.

It is guaranteed that there exists at least one solution satisfying all conditions. And if there are different solutions, print any one of them. The judge will regard two point's distance as R if it is within an absolute error of 10−3 of R.

 

Sample Input

1

7

1 1

1 0

1 -1

0 1

-1 1

0 -1

-1 0

Sample Output

0 0 1

 

思路:从n个点中随机3个点求出其共圆的圆心,判断其是否满足条件。

 

#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5;
struct Point
{
    double x,y;
}p[MAX];
double dis(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Point solve(Point a,Point b,Point c)//三点共圆圆心公式
{
    double x=( (a.x*a.x-b.x*b.x+a.y*a.y-b.y*b.y)*(a.y-c.y)-(a.x*a.x-c.x*c.x+a.y*a.y-c.y*c.y)*(a.y-b.y) ) / (2*(a.y-c.y)*(a.x-b.x)-2*(a.y-b.y)*(a.x-c.x));
    double y=( (a.x*a.x-b.x*b.x+a.y*a.y-b.y*b.y)*(a.x-c.x)-(a.x*a.x-c.x*c.x+a.y*a.y-c.y*c.y)*(a.x-b.x) ) / (2*(a.y-b.y)*(a.x-c.x)-2*(a.y-c.y)*(a.x-b.x));
    return (Point){x,y};
}
int main()
{
    int T,n;cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
        if(n==1||n==2){printf("%lf %lf %lf\n",p[0].x+1,p[0].y,1.0);continue;}
        if(n==3||n==4){printf("%lf %lf %lf\n",(p[1].x+p[2].x)/2,(p[1].y+p[2].y)/2,dis(p[1],p[2])/2);continue;}
        while(1)
        {
            int a=rand()%n;
            int b=rand()%n;
            int c=rand()%n;
            if(a==b||a==c||b==c)continue;
            if(fabs((p[b].y-p[a].y)*(p[c].x-p[b].x)-(p[c].y-p[b].y)*(p[b].x-p[a].x))<=1e-6)continue;
            Point q=solve(p[a],p[b],p[c]);
            int sum=0;
            for(int i=0;i<n;i++)
            {
                if(fabs(dis(p[i],q)-dis(p[a],q))<=1e-6)sum++;
            }
            if(2*sum>=n){printf("%lf %lf %lf\n",q.x,q.y,dis(p[a],q));break;}
        }
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值