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

来源:互联网 发布:linux 查看数据包 编辑:程序博客网 时间:2024/06/06 15:00

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(1N105).

The following N lines describe the points. Each line contains two real numbers Xi and Yi (0|Xi|,|Yi|103) indicating one give point. It's guaranteed that Npoints 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 PThree real numbers you output should satisfy 0|XP|,|YP|,R109.

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 103 of R.
 

Sample Input
171 11 01 -10 1-1 10 -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(abs(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;}


N2

阅读全文
1 0
原创粉丝点击