分析:
显然我们需要求出凸包
朴素的求法会在一开始读入所有点,并预处理使得前四个点不共面
然而这道题由于有输入顺序,我们只能在读入的时候暗箱操作
边读入边维护凸包
(dalao说这就是动态凸包)
每加入一个石子
若在凸包里,体积增量为 0,
若在外面我们可以发现,算法维护凸包删面的过程中,这个面和这个点构成的四面体就是体积增量的一部分,
所以我们只需要在删每个面的过程中求出这个四面体的体积并求和,就是第i个石子的增量
既然是动态的凸包,肯定在代码上有一点不同
cnt
c
n
t
:前
cnt
c
n
t
个点已经确定好了(不重合,不共线,不共面)
当cnt达不到3的时候,我们是不能维护凸包的
当加入第
i
i
个点后,
意味着我们已经找到了一个合法的初始四面体
那么我们把已有的点组成的凸包维护出来
此凸包的体积就是第
i
i
个点的贡献
(毕竟前个点都构不成立体图形,但是我们不能扔掉前面的点)
tip
一开始我发现vis数组的大小高达1e8,怀疑会开不下
然而好像没有这个问题
代码中没有这一小段
int tmp=num;
for (int i=num=0;i<tmp;i++)
if (F[i].ff)
F[num++]=F[i];
所以在求体积的时候,注意判断:if (F[i].ff)
WA了两次:
初始max_V=0,解决无法形成立体图形的特殊情况
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-8;
const int N=10001;
int opt;
double ans,V_pre;
struct node{
double x,y,z;
int id;
node (double xx=0,double yy=0,double zz=0) {
x=xx;y=yy;z=zz;
}
};
node operator +(const node &a,const node &b) {return node(a.x+b.x,a.y+b.y,a.z+b.z);}
node operator -(const node &a,const node &b) {return node(a.x-b.x,a.y-b.y,a.z-b.z);}
node operator *(const node &a,const double &b) {return node(a.x*b,a.y*b,a.z*b);}
node operator /(const node &a,const double &b) {return node(a.x/b,a.y/b,a.z/b);}
double Dot(node a,node b) {return a.x*b.x+a.y*b.y+a.z*b.z;}
node Cross(node a,node b) {return node(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x);}
double lenth(node a) {return sqrt(Dot(a,a));}
int dcmp(double x) {
if (fabs(x)<eps) return 0;
else if (x>0) return 1;
else return -1;
}
struct CH3D{
struct face{
int a,b,c;
bool ff;
};
int n,num;
node P[N];
face F[N*8];
int vis[N][N];
double add_V,max_V;
double lenth(node a) {
return sqrt(Dot(a,a));
}
double S(node a,node b,node c) {
return lenth(Cross(b-a,c-a));
}
double V(node a,node b,node c,node d) {
return Dot(d-a,Cross(b-a,c-a));
}
int cansee(node p,face f) {
double x=Dot(p-P[f.a],Cross(P[f.b]-P[f.a],P[f.c]-P[f.a]));
if (x>eps) return 1;
else return 0;
}
void deal(int p,int a,int b) {
int f=vis[a][b];
face add;
if (F[f].ff) {
if (cansee(P[p],F[f]))
dfs(p,f);
else {
add.a=b;
add.b=a;
add.c=p;
add.ff=1;
vis[b][a]=vis[a][p]=vis[p][b]=num;
F[num++]=add;
}
}
}
void dfs(int p,int now) {
F[now].ff=0;
add_V+=fabs(V(P[p],P[F[now].a],P[F[now].b],P[F[now].c]));
deal(p,F[now].b,F[now].a); //反
deal(p,F[now].c,F[now].b);
deal(p,F[now].a,F[now].c);
}
void create() {
int cnt=0;
int _=1;
num=0; max_V=0.0;
for (int i=0;i<n;i++) {
scanf("%lf%lf%lf",&P[i].x,&P[i].y,&P[i].z);
if (cnt==0) {
if (lenth(P[i]-P[0])>eps)
swap(P[1],P[i]),cnt++;
continue;
}
if (cnt==1) {
if (S(P[0],P[1],P[i])>eps)
swap(P[2],P[i]),cnt++;
continue;
}
if (cnt==2) {
if (fabs(V(P[0],P[1],P[2],P[i]))>eps)
swap(P[3],P[i]),cnt++;
if (cnt==3) {
face add;
_=i+1; //点的编号
for (int j=0;j<4;j++) {
add.a=(j+1)%4;
add.b=(j+2)%4;
add.c=(j+3)%4;
add.ff=1;
if (cansee(P[j],add)) swap(add.b,add.c);
vis[add.a][add.b]=vis[add.b][add.c]=vis[add.c][add.a]=num;
F[num++]=add;
}
for (int j=4;j<=i;j++)
for (int k=0;k<num;k++)
if (F[k].ff&&cansee(P[j],F[k])) {
dfs(j,k);
break;
}
max_V=Vsum(); //初始四面体构建好了
cnt++;
}
continue;
}
if (cnt>3) { //单点插入
add_V=0.0;
for (int j=0;j<num;j++)
if (F[j].ff&&cansee(P[i],F[j])) {
dfs(i,j);
break;
}
add_V/=6.0;
if (add_V>max_V)
max_V=add_V,_=i+1;
}
}
printf("%d %0.2lf\n",_,max_V);
}
double Vsum() {
node o(0,0,0);
double ans=0;
for (int i=0;i<num;i++)
if (F[i].ff) //判断该面是否在凸包上
ans+=V(P[F[i].a],P[F[i].b],P[F[i].c],o);
return fabs(ans/6.0);
}
};
CH3D hull;
int main()
{
int cas=0;
while (scanf("%d",&hull.n)!=EOF) {
printf("Case #%d:\n",++cas);
hull.create();
}
return 0;
}