[二分 几何] BZOJ 2289 【POJ Challenge】圆,圆,圆

%%%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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值