题解:P16143 [ICPC 2017 NAIPC] Stars in a Can

· · 题解

核心知识点:计算几何,三维计算几何

Part 1:分析圆柱体的可能形状

题目要求“圆柱体的至少一个底面上必须包含至少三颗恒星”,考虑这三个节点构成的面。整个圆柱体必然在这个面的一侧。而所有的节点都在这个圆柱体的内部,故所有的节点都在这个面的一侧。故这个面在这 n 个点构成的三维凸包上。

先求解出这 n 个点的三维凸包,由于 n 个点的三维凸包的面只有 O(n) 个,故可以考虑枚举每一个三维凸包的面作为圆柱体地面所在的面,然后确定最小的圆柱体。

Part 2:确定圆柱体形状

假设当前枚举的面由三个点 P_0,P_1,P_2 构成的面 \alpha,记 \vec{v}_1=\vec{P_0P_1},\vec{v}_2=\vec{P_0P_1}。

平面 \alpha 的法向量 \vec{n}=\dfrac{\vec{v_1}\times\vec{v_2}}{\left|\vec{v_1}\times\vec{v_2}\right|}。

圆柱体由底面和高构成,且它们在维度上独立,故我们可以分别考虑如何最小化它们。

最小化高

由于要将所有的点覆盖,故高就应当是所有点中距离 \alpha 最远的点的距离。

对于一个点 A,记 \vec{a}=\vec{P_0A},那么 A 点到 \alpha 的距离 d_A=\vec{a}\cdot \vec{n}。

遍历一遍取最大值即可。

最小化底面

此时不关心点到 \alpha 的距离,只需要其在 \alpha 上的投影在圆柱体地面的范围内即可。对于点 A,记其在 \alpha 上的投影位置为 A'。

如果求出了所有的点 A',它们仍然在空间直角坐标系 Oxyz 中的三维坐标(实际有效的只有两维)。这并不便于后续的计算,故想要在 \alpha 上从新建立一个平面直角坐标系 O'x'y'。

选取 O'=P_0,x' 轴正方向为 \hat{x}=\dfrac{\vec{v}_1}{|\vec{v}_1|},那么 y' 轴正方向 \hat{y} 需要垂直于 \hat{x} 与 \alpha 平面的法向量 \vec{n},直接选取 \hat{y}=\vec{n}\times \hat{x} 即可。

计算 A' 的坐标 (x'_A,y'_A),只需要将 \vec{P_0A'} 分别投影到 \hat{x} 和 \hat{y} 上即可,也就是 x'_A=\vec{P_0A'}\cdot\hat{x},y'_A=\vec{P_0A'}\cdot\hat{y}。

注意到 \vec{n}\cdot\hat{x}=\vec{n}\cdot\hat{y}=0 而 \vec{P_0A'}=\vec{P_0A}+\vec{AA'}=\vec{P_0A}-d_A\vec{n}。故 x'_A=\vec{P_0A}\cdot\hat{x},y'_A=\vec{P_0A}\cdot\hat{y},可以跳过预先计算 A' 的三维坐标的过程。

现在在 O'x'y' 坐标系中有 n 个点,需要选择一个底面(圆)覆盖所有的点,且其面积最小。这就是最小圆覆盖问题。随机排序之后使用增量算法,期望时间复杂度 O(n)。

总时间复杂度 O(n^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;
}