题意
给出n个点,找出经过一半点以上的圆。题目保证有解
分析
如果随机选择三个点,最差的情况下大概要0.5*0.5*0.5=0.125的概率选出正确的三个点求出正确的圆心。也就是选错的概率是7/8,那么重复实验一百次以上,错误率就会下降到1e-6以上,除非特别黑,不然很快能找到那个圆心的。判断一次要1e5的时间复杂度,勉强能在时限内完成
注意要特判5个一下点的特殊情况,那样可不是3个点来确定圆心了
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+10;
const double EPS = 1e-6;
int n;
struct Point
{
double x,y;
} p[MAXN];
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;
if(d<EPS) return {1e18,1e18};
return {a.x + (c1*b2 - c2*b1)/d, a.y + (a1*c2 -a2*c1)/d};
}
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool same(double x,double y)
{
return fabs(x-y)<EPS;
}
void printf(double x,double y,double r)
{
printf("%.6f %.6f %.6f\n",x,y,r);
}
void random(int k)
{
while(true)
{
int i=rand()%n;
int j=rand()%n;
int k=rand()%n;
Point o=waixin(p[i],p[j],p[k]);
if(abs(o.x)>1e9||abs(o.y)>1e9) continue;
int cnt=0;
double r=dis(p[i],o);
for(int i=0; i<n; i++)
{
if(same(dis(p[i],o),r))
{
cnt++;
if(cnt==k)
{
printf(o.x,o.y,r);
return ;
}
}
}
}
}
int main()
{
if (fopen("in.txt", "r") != NULL)
{
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
srand(time(0));
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
{
double x,y;
scanf("%lf%lf",&x,&y);
p[i]= {x,y};
}
if(n==1)
printf(p[0].x+1,p[0].y,1);
else if(n>=5)
random((n+1)/2);
else
printf((p[0].x+p[1].x)/2,(p[0].y+p[1].y)/2,dis(p[0],p[1])/2);
}
return 0;
}