1. 程式人生 > >2017 CCPC 哈爾濱站 --M --- Geometry Problem

2017 CCPC 哈爾濱站 --M --- Geometry Problem

題意就是: 二維平面上,給你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; }