(题目传送门)
迟到的模拟赛补题。
考场上二分写 shi 了,于是学习一下优秀的二分写法。
做法很显然,圆心必然在线段的中垂线上,预处理与每个圆相交的圆心的在中垂线上的范围,打到数轴上,最后扫描线。
自己写时对二分预处理圆心范围的讨论过于复杂,结合计算几何的知识,运用同向法可大大减少分讨难度。
#include<bits/stdc++.h>
#define db double
using namespace std;
const int N=1e5+10;
const db pi=acos(-1),eps=1e-8,INF=1e12;
int sign(db x){return x<eps? (-x<eps? 0:-1):1;}
struct Point
{
db x,y;
Point operator + (const Point &B) const
{return (Point){x+B.x,y+B.y};}
Point operator - (const Point &B) const
{return (Point){x-B.x,y-B.y};}
Point operator * (const db &B) const
{return (Point){x*B,y*B};}
Point operator / (const db &B) const
{return (Point){x/B,y/B};}
db operator | (const Point &B) const
{return (x*B.x)+(y*B.y);}
db operator ^ (const Point &B) const
{return (x*B.y)-(y*B.x);}
};
db dis(Point A,Point B){return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));}
int n;
db R,ans=INF;
Point A,B,C,k,P;
struct seq{db x;int op;}q[N<<1]; int cnt;
db find1(Point P,bool flag)
{
db lo=-INF,hi=INF;
for(int i=1; i<=80; i++)
{
db mid=(lo+hi)/2.000;
Point O=C+(k*mid);
if((dis(P,O)-R>dis(A,O))^flag)
lo=mid;
else
hi=mid;
}
return lo;
}
db find2(Point P,bool flag)
{
db lo=-INF,hi=INF;
for(int i=1; i<=80; i++)
{
db mid=(lo+hi)/2.000;
Point O=C+(k*mid);
if((dis(A,O)-dis(P,O)>R)^flag)
hi=mid;
else
lo=mid;
}
return hi;
}
int main()
{
scanf("%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y);
C=(A+B)/2.000;
k=(B-A)/dis(A,B); k=(Point){-k.y,k.x};
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%lf%lf%lf",&P.x,&P.y,&R);
bool flag=((P-A)^(B-P))>0;
q[++cnt]={find1(P,flag),flag?-1:1};
q[++cnt]={find2(P,flag),flag?1:-1};
}
q[++cnt]={0,0}; q[++cnt]={-INF,0}; q[++cnt]={INF,0};
sort(q+1,q+1+cnt,[](seq A,seq B){return A.x<B.x;});
int sum=0;
for(int i=1; i<=cnt; i++)
{
if(!sum)
ans=min(ans,fabs(q[i].x));
sum+=q[i].op;
if(!sum)
ans=min(ans,fabs(q[i].x));
}
Point O=C+(k*ans);
printf("%.10lf\n",dis(A,O));
return 0;
}
标签:cnt,CF887E,int,db,Little,Brother,++,flag,INF
From: https://www.cnblogs.com/xishanmeigao/p/18004403