2017 CCPC 哈尔滨站 --M --- Geometry Problem

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/My_stage/article/details/78690294

https://www.nowcoder.com/acm/contest/19/M

题意就是: 二维平面上,给你n个点,问是否有一个有一个点p,使得至少有 (n+1)/2向下取整个点他们与p点的距离为R。

思路: 采用随机化算法,算计3个点确定一个圆,那么我们遍历所有的点,看是否满足条件。只要不是非酋随机化之后确定一下精度就可以了。

#include <bits/stdc++.h>
#define maxs 2202002
#define ll long long int
#define mme(i,j) memset(i,j,sizeof(i))
using namespace std;
struct Point
{
    double x,y;
    Point(double _x,double _y){
        x=_x;
        y=_y;
    }
    Point(){}
}o[maxs];

Point waixin(Point a,Point b,Point c)
{
    double a1=b.x-a.x,b1=b.y-a.y,c1=(a1*a1+b1*b1)/2;
    double a2=c.x-a.x,b2=c.y-a.y,c2=(a2*a2+b2*b2)/2;
    double d=a1*b2-a2*b1;
    return Point(a.x+(c1*b2-c2*b1)/d,a.y+(a1*c2-a2*c1)/d);
}

double distant(Point a,Point b){
    return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}

void Solve(int n)
{
    int a,b,c,k;
    while(1)
    {
        k=0;
        a = rand()%n,b=rand()%n,c=rand()%n;
        Point cc = waixin(o[a],o[b],o[c]);
        double dis = distant(cc,o[a]);
        if(fabs(cc.x)>1e9||fabs(cc.y)>1e9||fabs(dis)>1e9) continue;

        for(int i=0;i<n;i++){
            double tmp = distant(o[i],cc);
            if( fabs(tmp-dis)<=1e-6 )
                k++;
            if(k==(n+1)/2){
                 printf("%.10lf %.10lf %.10lf\n",cc.x,cc.y,dis);
                break;
            }
        }
        if(k==(n+1)/2) break;
    }
}


int main()
{

    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&o[i].x,&o[i].y);
        if(n<5){
            if(n==1){
                     printf("%.10lf %.10lf %.10lf\n",o[0].x+1,o[0].y,1.0);
            }else{
                printf("%.10lf %.10lf %.10lf\n",(o[0].x+o[1].x)/2,(o[0].y+o[1].y)/2,distant(o[1],o[0])/2);

            }
        }else{
            Solve(n);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/My_stage/article/details/78690294