洛谷P2287 [HNOI2004]最佳包裹(三维凸包)

本文深入探讨了三维空间中凸包算法的实现细节,通过使用点结构和节点结构,介绍了如何构建和维护三维凸包。文章详细解释了如何通过点的初始化、面的更新以及顶点可见性的检查来逐步构建凸包,最终计算出凸包的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

传送门

题解

左转板子,调个精度就能\(A\)

//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
    R int res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
double readdb()
{
    R double x=0,y=0.1,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(x=ch-'0';(ch=getc())>='0'&&ch<='9';x=x*10+ch-'0');
    for(ch=='.'&&(ch=getc());ch>='0'&&ch<='9';x+=(ch-'0')*y,y*=0.1,ch=getc());
    return x*f;
}
const int N=105;const double eps=1e-10;
inline double Rd(){return 1.0*rand()/RAND_MAX;}
inline double reps(){return (Rd()-0.5)*eps;}
struct point{
    double x,y,z;
    point(){}
    point(R double xx,R double yy,R double zz):x(xx),y(yy),z(zz){}
    inline void init(){x=readdb()+reps(),y=readdb()+reps(),z=readdb()+reps();}
    inline point operator -(const point &b)const{return point(x-b.x,y-b.y,z-b.z);}
    inline point operator *(const point &b)const{return point(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);}
    inline double operator ^(const point &b)const{return x*b.x+y*b.y+z*b.z;}
    inline double norm()const{return sqrt(x*x+y*y+z*z);}
}p[N];
struct node{
    int id[3];
    node(){}
    node(R int x,R int y,R int z){id[0]=x,id[1]=y,id[2]=z;}
    inline point normal(){return (p[id[1]]-p[id[0]])*(p[id[2]]-p[id[0]]);}
    inline double area(){return normal().norm()/2;}
    inline bool ck(const point &b){return ((b-p[id[0]])^normal())>0;}
}f[N],st[N];
int vis[N][N],n,top,cnt,v;double res;
void Convex_3D(){
    f[++cnt]=node(1,2,3),f[++cnt]=node(3,2,1);
    fp(i,4,n){
        top=0;
        fp(j,1,cnt){
            v=f[j].ck(p[i]),!v?(st[++top]=f[j],0):0;
            vis[f[j].id[0]][f[j].id[1]]=
            vis[f[j].id[1]][f[j].id[2]]=
            vis[f[j].id[2]][f[j].id[0]]=v;
        }
        fp(j,1,cnt){
            if(vis[f[j].id[0]][f[j].id[1]]&&!vis[f[j].id[1]][f[j].id[0]])st[++top]=node(i,f[j].id[0],f[j].id[1]);
            if(vis[f[j].id[1]][f[j].id[2]]&&!vis[f[j].id[2]][f[j].id[1]])st[++top]=node(i,f[j].id[1],f[j].id[2]);
            if(vis[f[j].id[2]][f[j].id[0]]&&!vis[f[j].id[0]][f[j].id[2]])st[++top]=node(i,f[j].id[2],f[j].id[0]);
        }
        fp(j,1,top)f[j]=st[j];cnt=top;
    }
    fp(i,1,cnt)res+=f[i].area();
    printf("%.6lf\n",res);
}
int main(){
//  freopen("testdata.in","r",stdin);
    n=read();
    fp(i,1,n)p[i].init();
    Convex_3D();
    return 0;
}

转载于:https://www.cnblogs.com/bztMinamoto/p/10517053.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值