BZOJ 2823: [AHOI2012]信号塔

时间:2023-11-20 23:12:26

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2823

随机增量法。不断加点维护圆,主要是三点共圆那里打得烦(其实也就是个两中垂线求交点+联立方程求交点而已TAT。。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 1000009
#define eps 1e-6
using namespace std;
typedef unsigned long long ll;
struct P{double x,y;
}a[maxn],o;
double r;
int n;
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)){
if (ch=='-') f=-; ch=getchar();
}
while (isdigit(ch)){
x=x*+ch-''; ch=getchar();
}
return x*f;
}
P get2(P a,P b){
P tmp;
tmp.x=(a.x+b.x)/; tmp.y=(a.y+b.y)/;
return tmp;
}
double sqr(double x){
return x*x;
}
double dis(P a,P b){
return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
P cross(double a1,double a2,double b1,double b2,double c1,double c2){
P tmp;
tmp.x=(c2*b1-c1*b2)/(a1*b2-a2*b1); tmp.y=(c2*a1-c1*a2)/(b1*a2-b2*a1);
return tmp;
}
P get3(P a,P b,P c){
P tmp;
double a1=b.x-a.x,b1=b.y-a.y,c1=(sqr(a.y)-sqr(b.y)+sqr(a.x)-sqr(b.x))/;
double a2=c.x-a.x,b2=c.y-a.y,c2=(sqr(a.x)+sqr(a.y)-sqr(c.x)-sqr(c.y))/;
return tmp=cross(a1,a2,b1,b2,c1,c2);
}
int cmp(double x){
if (fabs(x)<eps) return ;
if (x>) return ;
return -;
}
int main(){
n=read();
rep(i,,n){
scanf("%lf%lf",&a[i].x,&a[i].y);
}
int x,y;
rep(i,,n) {
x=rand()%n+; y=rand()%n+;
swap(a[x],a[y]);
}
o=a[]; r=;
rep(i,,n){
if (cmp(dis(a[i],o)-r)<) continue;
o=a[i]; r=;
rep(j,,i-){
if (cmp(dis(a[j],o)-r)<) continue;
o=get2(a[i],a[j]); r=dis(a[j],o);
rep(k,,j-) {
if (cmp(dis(a[k],o)-r)<) continue;
o=get3(a[i],a[j],a[k]); r=dis(a[k],o);
}
}
}
printf("%.2lf %.2lf ",o.x,o.y);
printf("%.2lf\n",r);
return ;
}