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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值