//题目链接:http://zuojie.3322.org:88/soj/problem.action?id=4042
题目:
Description
A travelling circus faces a tough challenge in designing the dome for its performances. The circus has a number of shows that happen above the stage in the air under the dome. Various rigs, supports, and anchors must be installed over the stage, but under the dome. The dome itself must rise above the center of the stage and has a conical shape. The space under the dome must be air-conditioned, so the goal is to design the dome that contains minimal volume. You are given a set of n points in the space; (xi, yi, zi) for 1 <= i <= n are the coordinates of the points in the air above the stage that must be covered by the dome. The ground is denoted by the plane z = 0, with positive z coordinates going up. The center of the stage is on the ground at the point (0, 0, 0). The tip of the dome must be located at some point with coordinates (0, 0, h) with h > 0. The dome must have a conical shape that touches the ground at the circle with the center in the point (0, 0, 0) and with the radius of r. The dome must contain or touch all the n given points. The dome must have the minimal volume, given the above constraints.Input
The first line of the input file contains a single integer number n (1 <= n <= 10 000) — the number of points under the dome. The following n lines describe points with three floating point numbers xi, yi, and zi per line — the coordinates of i-th point. All coordinates do not exceed 1000 by their absolute value and have at most 2 digits after decimal point. All zi are positive. There is at least one point with non-zero xi or yi.Output
Write to the output file a single line with two floating point numbers h and r — the height and the base radius of the dome. The numbers must be precise up to 3 digits after decimal point.Sample Input
1 1.00 0.00 1.00 2 1.00 0.00 1.00 0.00 1.50 0.50 3 1.00 0.00 1.00 0.00 1.50 0.50 -0.50 -0.50 1.00Sample Output
3.000 1.500 2.000 2.000 2.000 2.000Source
NEERC2010
方法就是先把这一系列的三维坐标点进行降维。
我们可以把所有的点降为一些2维平面上的点
然后,圆锥的体积就变成了面积。这样简化了代码的复杂度
另外我们通过三分技术去寻找那个体积最小的圆锥。
这样,效率也非常高。
我的代码:
#include<stdio.h> #include<algorithm> #include<math.h> #define eps 1e-8 using namespace std; struct point { double x; double y; }; point p[10005]; int n; double maxx; point changetwo(double x,double y,double z) { point tmp,ret; tmp.x=x,tmp.y=y; ret.y=z; ret.x=sqrt(tmp.x*tmp.x+tmp.y*tmp.y); return ret; } double max(double a,double b) { if(a>b) return a; else return b; } double cul(double x) { int i; double ret,h=0; for(i=1;i<=n;i++) h=max(h,p[i].y*x/(x-p[i].x)); return h; } void solve(int n) { int i; double R,H,v1,v2; double left,right,mid1,mid2,h1,h2; left=maxx,right=10e9; for(i=1;i<=100;i++) { mid1=(2*left+right)/3; mid2=(left+2*right)/3; h1=cul(mid1); h2=cul(mid2); v1=mid1*mid1*h1; v2=mid2*mid2*h2; if(v1>v2) { left=mid1; } else { right=mid2; } } R=left; H=cul(left); printf("%.3lf %.3lf\n",H,R); } int main() { int i; double x,y,z; while(scanf("%d",&n)!=EOF) { maxx=0; for(i=1;i<=n;i++) { scanf("%lf%lf%lf",&x,&y,&z); p[i]=changetwo(x,y,z); if(p[i].x>maxx) maxx=p[i].x; } solve(n); } return 0; } /* 1 1.00 0.00 1.00 2 1.00 0.00 1.00 0.00 1.50 0.50 3 1.00 0.00 1.00 0.00 1.50 0.50 -0.50 -0.50 1.00 */