您的位置:首页 > 其它

HDU 3756 Dome of Circus(计算几何 + 三分)

2016-09-12 19:29 435 查看
题意:给定三维空间中n个点坐标,必在xOy平面之上,求一个最小的圆锥能包含所有顶点,点可以刚好在面上

思路:转换为平面上的点被一个三角形包围,Y坐标为原来的z坐标,X坐标为原来到z轴的距离,然后三分求解斜率,注意精度问题

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#include<iostream>
typedef long long ll;
const int maxn = 1e4 + 10;
const double eps = 1e-7;
const ll INF = 1e18;
using namespace std;

double x, y, z;
double X[maxn], Y[maxn];
int n, k, T;

int main()
{
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%lf %lf %lf", &x, &y, &z);
X[i] = sqrt(x * x + y * y);
Y[i] = z;
}
double res = INF, l = -INF, r = 0;
double R, H;
while(l < r) {
double res1 = 0, res2 = 0;
double rr1, hh1, rr2, hh2;
double mid = (l + r) / 2, mmid = (mid + r) / 2;
for(int i = 0; i < n; i++) {
double b1 = Y[i] - mid * X[i], c1 = (-b1) / mid;
double b2 = Y[i] - mmid * X[i], c2 = (-b2) / mmid;
if(res1 < b1 * c1 * c1) { rr1 = c1; hh1 = b1; res1 = b1 * c1 * c1; }
if(res2 < b2 * c2 * c2) { rr2 = c2; hh2 = b2; res2 = b2 * c2 * c2; }
}
if(res > res1) {
res = res1; R = rr1; H = hh1;
}
if(res > res2) {
res = res2; R = rr2; H = hh2;
}
if(res1 <= res2) r = mmid - eps;
else l = mid + eps;
}
printf("%.3f %.3f\n", H, R);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: