题意:
给n<10000个在z轴上方的点
求一个全包含了所有点的最小体积锥形
解法:
把x y z映射成sqrt(x*x+y*y),z 即可转化为二维坐标
然后求一个全包含了所有点的三角形
使得底边的平方*高最小
三分高,暴力求底即可
//大白p263
#include <cmath>
#include <cstdio>
#include <cstring>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const double eps=1e-9;//精度
const int INF=1<<29;
const double PI=acos(-1.0);
int dcmp(double x){//判断double等于0或。。。
if(fabs(x)<eps)return 0;else return x<0?-1:1;
}
struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}//向量+向量=向量
Vector operator-(Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}//点-点=向量
Vector operator*(Vector a,double p){return Vector(a.x*p,a.y*p);}//向量*实数=向量
Vector operator/(Vector a,double p){return Vector(a.x/p,a.y/p);}//向量/实数=向量
double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}//|a|*|b|*cosθ 点积
double Length(Vector a){return sqrt(Dot(a,a));}//|a| 向量长度
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}//叉积 向量围成的平行四边形的面积
Point GetLineIntersection(Point p,Vector v,Point q,Vector w){//求直线交点 有唯一交点时可用
Vector u=p-q;
double t=Cross(w,u)/Cross(v,w);
return p+v*t;
}
//--------------------------------------
//--------------------------------------
//--------------------------------------
//--------------------------------------
//--------------------------------------
Point arr[10010];
int n,T;
const double maxh=4000.0;
double calmaxv(double h){
double maxv=0,temp,r;
for(int i=0;i<n;i++){
r=GetLineIntersection(Point(0,h),Vector(arr[i]-Point(0,h)),Point(0,0),Vector(1,0)).x;
temp=h*r*r;
maxv=max(maxv,temp);
}
return maxv;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
double tx,ty,tz;
double lowh=0;
for(int i=0;i<n;i++){
scanf("%lf%lf%lf",&tx,&ty,&tz);
arr[i].x=Length(Vector(tx,ty));
arr[i].y=tz;
lowh=max(lowh,tz);
}
double left=lowh,right=maxh;
while(dcmp(right-left)>0){
double leftmid=(left*2+right)/3;
double rightmid=(left+right*2)/3;
if(dcmp(calmaxv(leftmid)-calmaxv(rightmid))>0)
left=leftmid;
else right=rightmid;
}
double ans=calmaxv(left);
ans=sqrt(ans/left);
printf("%.3lf %.3lf\n",left,ans);
}
return 0;
}