Weird Flecks, But OK

Weird Flecks, But OK

题意:

给出三维坐标中的 n 个点,求一个圆柱的最小直径,该圆柱垂直于坐标平面且能覆盖住所有点

题解:

本人最不擅长计算几何,比赛时没做出来。。。
其实就是将n个点投影到三个坐标平面,在三个坐标面上分别做期望时间复杂度为O(n)的最下圆覆盖算法,最后取最小值
将三维转为二维问题

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
using namespace std;
int read()
{
    
    
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
    
    
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
    
    
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 2e5+5;
const int maxm = 3e4+5;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
int n;
struct Point{
    
    
	double x,y;
}a[maxn],b[maxn],c[maxn];
double get_dis(Point a,Point b)
{
    
    
	return hypot(a.x-b.x,a.y-b.y);
}
void updat(Point p1,Point p2,Point p3,Point &o,double &r) //用确定的三点更新外接圆圆心和半径 
{
    
    
	double xx1 = 2*(p2.x-p1.x),yy1 = 2*(p2.y-p1.y);
    double c1 = p2.x*p2.x+p2.y*p2.y-p1.x*p1.x-p1.y*p1.y;
    double xx2 = 2*(p3.x-p1.x),yy2 = 2*(p3.y-p1.y);
    double c2 = p3.x*p3.x+p3.y*p3.y-p1.x*p1.x-p1.y*p1.y;
    o.x = (yy1*c2-yy2*c1)/(yy1*xx2-yy2*xx1);
    o.y = (xx2*c1-xx1*c2)/(yy1*xx2-yy2*xx1);
    r=get_dis(o,p1);
}
double get_circle(Point a[]) //求能覆盖a中所有点的最小外接圆直径 
{
    
    
	random_shuffle(a+1,a+n+1);
	Point o = a[1];double r = 0;
	for(int i = 2;i <= n;i++)
	  	if(get_dis(o,a[i]) > r+eps)
	  	{
    
    
	  		o = a[i],r = 0;
	  		for(int j = 1;j < i;j++)
	  			if(get_dis(o,a[j]) > r+eps)
	  			{
    
    
	  				o.x = (a[i].x+a[j].x)/2;
	  				o.y = (a[i].y+a[j].y)/2;
	  				r = get_dis(a[i],a[j])/2;
	  				for(int k = 1;k < j;k++)
	  					if(get_dis(o,a[k]) > r+eps) 
	  						updat(a[i],a[j],a[k],o,r);
				}
	 	}
	return 2*r;
}
signed main()
{
    
    
	n = read();
	for(int i = 1;i <= n;i++)
	{
    
    
		double x,y,z;
		scanf("%lf%lf%lf",&x,&y,&z);
		a[i] = {
    
    x,y};b[i] = {
    
    y,z};c[i] = {
    
    x,z};
	}
	double ans;
	ans = min({
    
    get_circle(a),get_circle(b),get_circle(c)});
	printf("%.10f\n",ans);
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_35975367/article/details/115297417
OK