%%%fhq http://fanhq666.blog.163.com/blog/static/81943426201131691852787/
这题的算法:二分相交部分的横坐标。
用一条直线x=x0和所有圆计算交点,如果有的圆不和它相交,易知交集和直线的左右关系。
如果所有圆和直线的相交的部分的交集非空,那么输出yes。
如果某两个圆和直线的相交部分不相交,那么这两个圆的交点肯定在直线的同侧,由此可得左右关系。
这样就可以了。
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef pair<double,double> abcd;
double sqr(double x) { return x*x; }
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y) { }
void read(){
scanf("%lf%lf",&x,&y);
}
};
const int N=100005;
struct Circle{
Point c; double r;
void read(){
c.read(); scanf("%lf",&r);
}
abcd cross(double x0){
double d=sqrt(sqr(r)-sqr(x0-c.x)+1e-15);
return abcd(c.y-d,c.y+d);
}
}C[N];
bool Cross(Circle C1,Circle C2,Point &p1,Point &p2){
double r1=C1.r,r2=C2.r;
double l2=sqr(C1.c.x-C2.c.x)+sqr(C1.c.y-C2.c.y);
if (l2>sqr(r1+r2))return 0;
double l=sqrt(l2);
double cs0=(C2.c.x-C1.c.x)/l,sn0=(C2.c.y-C1.c.y)/l;
double cs1=(r1*r1+l2-r2*r2)*0.5/r1/l,sn1=sqrt(1.0-cs1*cs1);
p1=Point(C1.c.x+r1*(cs0*cs1-sn0*sn1),C1.c.y+r1*(cs0*sn1+cs1*sn0));
p2=Point(C1.c.x+r1*(cs0*cs1+sn0*sn1),C1.c.y+r1*(-cs0*sn1+cs1*sn0));
return 1;
}
int n;
double L=-1e130,R=1e130,MID;
int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++){
C[i].read();
L=max(L,C[i].c.x-C[i].r);
R=min(R,C[i].c.x+C[i].r);
}
if (L>=R) return printf("NO\n"),0;
int Case=20;
while (Case--)
{
MID=(L+R)/2;
int a=0,b=0; double h,l;
for (int i=1;i<=n;i++){
abcd cro=C[i].cross(MID);
if (!a || cro.first>l) l=cro.first,a=i;
if (!b || cro.second<h) h=cro.second,b=i;
}
if (h-l>1e-9) return printf("YES\n"),0;
Point p1,p2;
if (!Cross(C[a],C[b],p1,p2))
return printf("NO\n"),0;
p1.x<MID?R=MID:L=MID;
}
return printf("NO\n"),0;
}