跳到正文

题解

AT_abc314_h [ABC314Ex] Disk and Segments 题解

Comet387约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

前言

赛时没看,后悔了,赛后看了之后 A 了。

思路

设选择的点是 (x,y)(x,y)。通过样例很容易发现最小半径关于 x,yx,y 的函数是两个单峰函数,于是考虑三分套三分(精度建议设为 ∣l−r∣≤10−12|l-r|\leq10^{-12}),对应代码:

C++
double div3(double x) // 三分 y
{
	double l=0,r=1000;
	while(abs(l-r)>1e-12)
	{
		double mid1=l+(r-l)/3,mid2=r-(r-l)/3;
		if(calc(x,mid1)>calc(x,mid2)) l=mid1;
		else r=mid2;
	}
	return (calc(x,l)+calc(x,r))/2;
}
double main_div3() // 三分 x
{
	double l=0,r=1000;
	while(abs(l-r)>1e-12)
	{
		double mid1=l+(r-l)/3,mid2=r-(r-l)/3;
		if(div3(mid1)>div3(mid2)) l=mid1;
		else r=mid2;
	}
	return (div3(l)+div3(r))/2;
}

然后,为了使圆半径最小、且需要保证使得任意一条线段都和圆周或圆内部有至少一个公共点,圆半径需要选择 (x,y)(x,y) 与给定线段的最大距离,对应代码:

C++
double dist_point2seg(double ptx,double pty,double sgx1,double sgy1,double sgx2,double sgy2)
// 计算点 (ptx,pty) 与端点为 (sgx1,sgy1)、(sgx2,sgy2) 的线段的距离
{
	double cross=(sgx2-sgx1)*(ptx-sgx1)+(sgy2-sgy1)*(pty-sgy1); // 叉积
	if(cross<=0) return hypot(ptx-sgx1,pty-sgy1);
   // hypot(x,y) 代表 sqrt(x*x+y*y)
	double dis=sqr(hypot(sgx2-sgx1,sgy2-sgy1));
	if(cross>=dis) return hypot(ptx-sgx2,pty-sgy2);
	double r=cross/dis,px=sgx1+(sgx2-sgx1)*r,py=sgy1+(sgy2-sgy1)*r;
	return hypot(ptx-px,pty-py);
}
double calc(double x,double y) // 计算半径
{
	double ans=0;
	rep(i,1,n) ans=max(ans,dist_point2seg(x,y,x1[i],y1[i],x2[i],y2[i]));
	return ans;
}

最后这道题就 AC 了,完整代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define re register
#define rep(i,a,b) for(re int i(a);i<=b;++i)
#define req(i,a,b) for(re int i(a);i>=b;--i)
inline int read()
{
    re int x=0; re bool f=1; re char ch=getchar();
    while(!isdigit(ch)) f=ch=='-'?0:f,ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?x:-x;
}
inline void write(int x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+48);
}
#define y1 Y1
#define y2 Y2
int n,x1[101],y1[101],x2[101],y2[101];
double sqr(double x){return x*x;}
double dist_point2seg(double ptx,double pty,double sgx1,double sgy1,double sgx2,double sgy2)
{
	double cross=(sgx2-sgx1)*(ptx-sgx1)+(sgy2-sgy1)*(pty-sgy1);
	if(cross<=0) return hypot(ptx-sgx1,pty-sgy1);

	double dis=sqr(hypot(sgx2-sgx1,sgy2-sgy1));
	if(cross>=dis) return hypot(ptx-sgx2,pty-sgy2);
	double r=cross/dis,px=sgx1+(sgx2-sgx1)*r,py=sgy1+(sgy2-sgy1)*r;
	return hypot(ptx-px,pty-py);
}
double calc(double x,double y)
{
	double ans=0;
	rep(i,1,n) ans=max(ans,dist_point2seg(x,y,x1[i],y1[i],x2[i],y2[i]));
	return ans;
}
double div3(double x)
{
	double l=0,r=1000;
	while(abs(l-r)>1e-12)
	{
		double mid1=l+(r-l)/3,mid2=r-(r-l)/3;
		if(calc(x,mid1)>calc(x,mid2)) l=mid1;
		else r=mid2;
	}
	return (calc(x,l)+calc(x,r))/2;
}
double main_div3()
{
	double l=0,r=1000;
	while(abs(l-r)>1e-12)
	{
		double mid1=l+(r-l)/3,mid2=r-(r-l)/3;
		if(div3(mid1)>div3(mid2)) l=mid1;
		else r=mid2;
	}
	return (div3(l)+div3(r))/2;
}
int main()
{
	n=read();
	rep(i,1,n) x1[i]=read(),y1[i]=read(),x2[i]=read(),y2[i]=read(); // 分别对应题目中的 a,b,c,d
	printf("%.100lf",main_div3());
	return 0;
}

后记

  • 这道题可以把三分换成模拟退火,但是被卡了……
  • AtCoder 更新了可选语言及其标识符,洛谷交上去 UKE 了,希望早日修复。
原文