跳到正文

题解

题解 #10105 Circular Parterre

Diaоsi约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月3日
篇幅
约 15 分钟
获赞
2

版本

  1. v1当前

Circular Parterre

省流:注意到答案一定 ≥rmax⁡\geq r_{\max},所以原本的圆的圆弧不会对答案圆的圆弧造成限制,限制圆大小的一定是圆并上的顶点。把圆并的空弧拉直之后这题就变成了 [ICPC 2018 WF] Panda Preserve,然后就做完了。

首先将圆并的顶点集合 PP 求出来,这一步可以通过极角排序 + 扫描线完成。

接下来考虑圆心可能存在的位置:求出 PP 的 Voronoi Diagram。这玩意是啥呢,就是将平面划分成 ∣P∣|P| 个区域,区域 RiR_i 满足对于 x∈Ri, d(x,pi):=∥x−pi∥2\bm{x}\in R_i,\,d(\bm{x},p_i):=\|\bm{x}-\bm{p}_i\|^2 最小。也就是说区域 RiR_i 内的点在欧氏距离下离 pi\bm{p}_i 最近。

图形化:

VD

每个区域内生成的圆心一定会被管辖该区域的点阻挡,注意到这些区域是凸集,所以能使得半径最大的位置一定在凸包的端点上。Voronoi Diagram 的朴素求法是:求出每个点与其他点的中垂线,再求中垂线的半平面交,得到的区域就是 Voronoi Diagram 的 Cell。由于 Voronoi Diagram 是平面图,所以其顶点只有 O(∣P∣)\mathcal{O}(|P|) 个。

那么 ∣P∣|P| 是多大呢?记初始的圆集合为 C:={(ci,ri)}C:=\{(\bm{c}_i,r_i)\},把距离定义为 d(x,pi):=∥x−ci∥2−ri2d(\bm{x},p_i):=\|\bm{x}-\bm{c}_i\|^2-r_i^2,在这个距离下求出 Voronoi Diagram,任意两个互不包含的圆都会被划分到不同的 Cell 中。这种 Voronoi Diagram 在学术上被称作 Power Diagram。

图形化:

PD

由于 Power Diagram 至多产生两个圆并上的顶点,因此圆并的顶点不会超过 6n−126n-12 个。进一步地,我们求出的 Voronoi Diagram 上的顶点不会超过 18n−3618n-36 个。

接下来用 Voronoi Diagram 上的顶点更新答案,需要对每个顶点判断是否合法。可以用卷绕数法或者射线法判断该顶点是否在圆并圆弧拉直后的多边形内,因为在多边形外的点半径一定不大于 rmax⁡r_{\max}。注意这个多边形不一定是简单的(可能有洞)但是一定是不自交的。

接下来分析一下时间复杂度,求圆并和 Voronoi Diagram 的朴素做法都是 O(n2log⁡n)\mathcal{O}(n^2\log n) 的,两者都可以用 Voronoi Diagram 的分治求法做到 O(nlog⁡n)\mathcal{O}(n\log n)。最后对 Voronoi Diagram 的每个顶点检查是否在多边形内的复杂度是 O(n2)\mathcal{O}(n^2),也可以通过扫描线做平面图点定位做到 O(nlog⁡n)\mathcal{O}(n\log n)。

实现了 O(n2log⁡n)\mathcal{O}(n^2\log n) 的代码。

C++
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define poly vector<node>
#define segs vector<line>
typedef long long ll;
typedef long double ld;
using namespace std;
const int N=1010,V=2000,INF=0x3f3f3f3f;
const ld eps=1e-9,pi=acos(-1),inf=1e18;

mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

int dcmp(ld x){return x<-eps?-1:(x>eps?1:0);}

struct node{
	ld x,y;
	node(ld xx=0,ld yy=0){x=xx,y=yy;}
	bool operator <(const node &a)const{return dcmp(x-a.x)?x<a.x:y<a.y;}
	bool operator ==(const node &a)const{return !dcmp(x-a.x)&&!dcmp(y-a.y);}
	node &operator +=(const node &b){x+=b.x,y+=b.y;return *this;}
	node &operator -=(const node &b){x-=b.x,y-=b.y;return *this;}
	node &operator *=(const ld &b){x*=b,y*=b;return *this;}
	node &operator /=(const ld &b){x/=b,y/=b;return *this;}
	friend istream &operator >>(istream &is,node &a){return is>>a.x>>a.y;}
	friend ostream &operator <<(ostream &os,node &a){return os<<'('<<a.x<<','<<a.y<<')';}
}O(0,0);

node operator +(const node &a,const node &b){return node(a.x+b.x,a.y+b.y);}
node operator -(const node &a,const node &b){return node(a.x-b.x,a.y-b.y);}
node operator *(const ld &x,const node &a){return node(x*a.x,x*a.y);}
node operator /(const node &a,const ld &x){return node(a.x/x,a.y/x);}
ld operator *(const node &a,const node &b){return a.x*b.x+a.y*b.y;}
ld operator ^(const node &a,const node &b){return a.x*b.y-a.y*b.x;}

ld sqr(ld x){return x*x;}
ld len(node a){return sqrt(a*a);}
ld len2(node a){return a*a;}
ld dis(node a,node b){return len(a-b);}
ld atan2(node a){return std::atan2(a.y,a.x);}
node zoom(node a,ld x){return (x*a)/len(a);}
node normal(node a){return node(-a.y,a.x);}

node intersect(node a,node b,node c,node d){
	node x=b-a,y=d-c,z=a-c;
    return a+((y^z)/(x^y))*x;
}

bool contain(node a,node b,node c){
	node x=a-b,y=c-b;
	return (fabs(x^y)<eps)&&((x*y)<eps);
}

bool parallel(node a,node b,node c,node d){
	return !dcmp((b-a)^(d-c));
}

bool ole(node pa,node pb,node a){return dcmp((a-pa)^(pb-pa))<0;}
bool ori(node pa,node pb,node a){return dcmp((a-pa)^(pb-pa))>0;}

struct line{
	node a,b;
	ld k;
	line(node aa=O,node bb=O){a=aa,b=bb,k=atan2(b.y-a.y,b.x-a.x);}
	bool operator <(const line &o)const{return dcmp(k-o.k)?dcmp(k-o.k)<0:ole(o.a,o.b,a);}
	bool operator ==(const line &o)const{return !dcmp(k-o.k);}
};

node intersect(line a,line b){return intersect(a.a,a.b,b.a,b.b);}
bool ori(line p,node a){return dcmp((a-p.a)^(p.b-p.a))>0;}

line q[N];

poly halfcut(segs &v){
	sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()),v.end());
	int h=1,t=0;
	for(line z:v){
		while(h<t&&ori(z,intersect(q[t],q[t-1])))t--;
        while(h<t&&ori(z,intersect(q[h],q[h+1])))h++;
        q[++t]=z;
	}
	while(h<t&&ori(q[h],intersect(q[t],q[t-1])))t--;
    while(h<t&&ori(q[t],intersect(q[h],q[h+1])))h++;
    poly u;
    if(t-h<=1)return u;
    for(int i=h;i<=t;i++)u.pb(intersect(q[i],q[i==t?h:i+1]));
    return u;
}

bool inpoly(segs &p,node a){
	int res=0,n=(int)p.size();
	for(int i=0;i<n;i++){
		node s=p[i].a,t=p[i].b;
		if(contain(s,a,t))return 1;
		int d1=dcmp(s.y-a.y),
			d2=dcmp(t.y-a.y),
			k=dcmp((t-s)^(a-s));
        if(k>0&&d1<=0&&d2>0)res++;
        if(k<0&&d2<=0&&d1>0)res--;
	}
	return res;
}

struct circle{
	node o;
	ld r;
	circle(node oo=O,ld rr=0){o=oo,r=rr;}
	bool operator <(const circle &a)const{return o==a.o?r<a.r:o<a.o;}
	bool operator ==(const circle &a)const{return o==a.o&&!dcmp(r-a.r);}
	friend istream &operator >>(istream &is,circle &a){return is>>a.o>>a.r;}
	friend ostream &operator <<(ostream &os,circle &a){return os<<a.o<<' '<<a.r;}
};

int contain(circle c1,circle c2){
	node o1=c1.o,o2=c2.o,o=o2-o1;
	ld d2=len2(o),r1=c1.r,r2=c2.r;
    if(dcmp((r1+r2)*(r1+r2)-d2)<=0)return 0;
    if(dcmp((r1-r2)*(r1-r2)-d2)>=0)return dcmp(r1-r2)<=0?1:2;
    return 3;
}

void angle_intersection(circle c1,circle c2,ld &beg,ld &end){
	node o1=c1.o,o2=c2.o,o=o2-o1;
	ld d2=len2(o),d=sqrt(d2),r1=c1.r,r2=c2.r,
	cs=((r1+r2)*(r1-r2)+d2)/(2.0*d*r1),
	sg=acos(cs),ig=atan2(o);
	(ig<0)?ig+=2*pi:0;
	(beg=ig-sg)<0?beg+=2*pi:0;
	(end=ig+sg)>2*pi?end-=2*pi:0;
}

int T,n;
ld ans;
poly p,e,u;
segs v,h,tmp;
circle c[N];

void getans(node a){
	ld res=inf;
	for(int i=0;i<(int)p.size();i++)
		res=min(res,dis(a,p[i]));
	ans=max(ans,res);
}

void solve(){
	cin>>n;ans=0;
	p.clear();h.clear();
	for(int i=0;i<n;i++){
		cin>>c[i];
		ans=max(ans,c[i].r);
		if(!dcmp(c[i].r))i--,n--;
	}
	sort(c,c+n);
	n=unique(c,c+n)-c;
	for(int i=0;i<n;i++){
		int flg=0;
		e.clear();
		for(int j=0;j<n;j++)
			flg|=(i!=j&&contain(c[i],c[j])==1);
		if(flg)continue;
		for(int j=0;j<n;j++){
			if(i==j||contain(c[i],c[j])!=3)continue;
			ld l,r;
			angle_intersection(c[i],c[j],l,r);
			if(l<=r)e.pb(node(l,r));
			else e.pb(node(l,2*pi)),e.pb(node(0,r));
		}
		e.pb(node(2*pi,2*pi));
		sort(e.begin(),e.end());
		ld lst=0;
		for(node q:e)
			if(lst<q.x){
				ld a=lst,b=q.x,r=c[i].r;
				p.pb(c[i].o+node(r*cos(a),r*sin(a)));
				p.pb(c[i].o+node(r*cos(b),r*sin(b)));
				h.pb(line(p[p.size()-2],p[p.size()-1]));
				lst=q.y;
			}
			else if(lst<q.y)lst=q.y;
	}
	sort(p.begin(),p.end());
	p.erase(unique(p.begin(),p.end()),p.end());
	n=p.size();
	for(int i=0;i<n;i++){
		v=tmp;
		for(int j=0;j<n;j++){
			if(i==j)continue;
			node o=(p[i]+p[j])/2,dir=normal(p[j]-p[i]);
			v.pb(line(o-dir,o+dir));
		}
		u=halfcut(v);
		for(node q:u)
			if(inpoly(h,q))getans(q);
	}
	cout<<fixed<<setprecision(15)<<ans<<'\n';
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	tmp.pb(line(node(-V,-V),node(V,-V)));
	tmp.pb(line(node(V,-V),node(V,V)));
	tmp.pb(line(node(V,V),node(-V,V)));
	tmp.pb(line(node(-V,V),node(-V,-V)));
	cin>>T;
	while(T--)solve();
	return 0;
}