核心知识点:计算几何,三维计算几何
Part 1:分析圆柱体的可能形状
题目要求“圆柱体的至少一个底面上必须包含至少三颗恒星”,考虑这三个节点构成的面。整个圆柱体必然在这个面的一侧。而所有的节点都在这个圆柱体的内部,故所有的节点都在这个面的一侧。故这个面在这 个点构成的三维凸包上。
先求解出这 个点的三维凸包,由于 个点的三维凸包的面只有 个,故可以考虑枚举每一个三维凸包的面作为圆柱体地面所在的面,然后确定最小的矩形。
Part 2:确定圆柱体形状
假设当前枚举的面由三个点 ,, 构成的面 ,记 ,。
平面 的法向量 。
圆柱体由底面和高构成,且它们在维度上独立,故我们可以分别考虑如何最小化它们。
最小化高
由于要将所有的点覆盖,故高就应当是所有点中距离 最远的点的距离。
对于一个点 ,记 ,那么 点到 的距离 。
遍历一遍取最大值即可。
最小化底面
此时不关心点到 的距离,只需要其在 上的投影在圆柱体地面的范围内即可。对于点 ,记其在 上的投影位置为 。
如果求出了所有的点 ,它们仍然在空间直角坐标系 中的三维坐标(实际有效的只有两维)。这并不便于后续的计算,故想要在 上从新建立一个平面直角坐标系 。
选取 , 轴正方向为 ,那么 轴正方向 需要垂直于 与 平面的法向量 ,直接选取 即可。
计算 的坐标 ,只需要将 分别投影到 和 上即可,也就是 ,。
注意到 而 。故 ,,可以跳过预先计算 的三维坐标的过程。
现在在 坐标系中有 个点,需要选择一个底面(圆)覆盖所有的点,且其面积最小。这就是最小圆覆盖问题。随机排序之后使用增量算法,期望时间复杂度 。
总时间复杂度 。
这道题实际上就是两个板子缝起来的,实现代码:
#include <bits/stdc++.h>
using namespace std;
#define ld long double
const ld eps=1e-7;
const ld PI=acos(-1);
struct p2{
ld x,y;
ld norm(){return x*x+y*y;}
};
p2 operator+(p2 A,p2 B)
{
return p2{A.x+B.x,A.y+B.y};
}
p2 operator-(p2 A,p2 B)
{
return p2{A.x-B.x,A.y-B.y};
}
ld operator*(p2 A,p2 B)
{
return A.x*B.x+A.y*B.y;
}
ld operator/(p2 A,p2 B)
{
return A.x*B.y-A.y*B.x;
}
p2 operator*(p2 A,ld B)
{
return p2{A.x*B,A.y*B};
}
p2 operator/(p2 A,ld B)
{
return p2{A.x/B,A.y/B};
}
ld dist(p2 A,p2 B){return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));}
p2 r90(p2 A){return p2{-A.y,A.x};}
struct circle{
p2 O;ld r;
bool in(p2 A){return dist(O,A)<r+eps;}
};
circle get_c(p2 A,p2 B)
{
circle ret;
ret.O=(A+B)*0.5;
ret.r=dist(A,ret.O);
return ret;
}
circle get_c(p2 A,p2 B,p2 C)
{
circle ret;
p2 bc=C-B,ca=A-C,ab=B-A;
ret.O=(B+C-r90(bc)*(ca*ab)/(ca/ab))/2;
ret.r=dist(A,ret.O);
return ret;
}
struct p3{
ld x,y,z;
ld len(){return sqrt(x*x+y*y+z*z);}
};
p3 operator+(p3 A,p3 B)
{
return p3{A.x+B.x,A.y+B.y,A.z+B.z};
}
p3 operator-(p3 A,p3 B)
{
return p3{A.x-B.x,A.y-B.y,A.z-B.z};
}
ld operator*(p3 A,p3 B)
{
return A.x*B.x+A.y*B.y+A.z*B.z;
}
p3 operator*(p3 A,ld B)
{
return p3{A.x*B,A.y*B,A.z*B};
}
p3 operator/(p3 A,p3 B)
{
return p3{A.y*B.z-A.z*B.y,A.z*B.x-A.x*B.z,A.x*B.y-A.y*B.x};
}
struct plane{
p3 u[3];
plane(){}
plane(p3 A,p3 B,p3 C){u[0]=A,u[1]=B,u[2]=C;}
p3 normal(){return (u[1]-u[0])/(u[2]-u[0]);}
ld dist(p3 a){return (a-u[0])*normal()/normal().len();}
};
struct face{
int a[3];
plane p;
};
p3 p[1010];
face get_face(int i,int j,int k)
{
face ret;
ret.a[0]=i,ret.a[1]=j,ret.a[2]=k;
ret.p=plane(p[i],p[j],p[k]);
return ret;
}
int vis[1010][1010];
vector<face> get_hull(int n)
{
vector<face> o;
o.push_back(get_face(1,2,3));
o.push_back(get_face(1,3,2));
for(int i=4;i<=n;i++)
{
vector<face> tmp1,tmp2;
for(auto &g:o)
if(g.p.dist(p[i])<-eps) tmp1.push_back(g);
else
{
tmp2.push_back(g);
for(int j=0;j<3;j++)
vis[g.a[j]][g.a[(j+1)%3]]=i;
}
for(auto &g:tmp2)
for(int j=0;j<3;j++)
if(vis[g.a[(j+1)%3]][g.a[j]]!=i)
tmp1.push_back(get_face(g.a[j],g.a[(j+1)%3],i));
o=tmp1;
}
return o;
}
int n;
p2 pp[1010];
mt19937 rnd(20061128);
ld ans=1e13;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
cin>>p[i].x>>p[i].y>>p[i].z;
vector<face> o=get_hull(n);
for(face &g:o)
{
ld H=0.;
plane S=g.p;
p3 norm=S.normal(),
vecx=S.u[1]-S.u[0],
vecy;
norm=norm*(1./norm.len());
vecx=vecx*(1./vecx.len());
vecy=norm/vecx;
for(int i=1;i<=n;i++)
{
H=max(H,-S.dist(p[i]));
pp[i].x=(p[i]-S.u[0])*vecx,
pp[i].y=(p[i]-S.u[0])*vecy;
}
shuffle(pp+1,pp+n+1,rnd);
circle C=circle{pp[1],0};
for(int i=2;i<=n;i++) if(!C.in(pp[i]))
{
C=get_c(pp[i],pp[1]);
for(int j=2;j<i;j++) if(!C.in(pp[j]))
{
C=get_c(pp[i],pp[j]);
for(int k=1;k<j;k++) if(!C.in(pp[k]))
C=get_c(pp[i],pp[j],pp[k]);
}
}
ans=min(ans,H*C.r*C.r*PI);
}
printf("%.12Lf\n",ans);
return 0;
}