G - Dome of Circus UVALive - 4986 (三分)

https://vjudge.net/contest/123097#problem/G
三分最後一題
其實看上去像一個三維問題,分分鐘化成二維平面內的問題, 三分的是體積,我們只要比較r*r*h的大小就行,
下凸函數,自己畫一下圖感受一下就行了. 還是用斜率的角度走一下流程.

#include<cstdio>
#include<iostream>
#include<cmath>

const int maxn = 1e4+5;
const double eps = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int n;
double ansr, ansh;
struct Point{
    double x,y;
    Point(){}
    Point(double x, double y):x(x),y(y){}
}p[maxn];

double cal(double ang){
    double maxv = 0;
    double a,b;
    for(int i = 0; i < n; i++){
        b = p[i].x*tan(ang) + p[i].y;
        a = b/tan(ang);
        maxv = max(maxv, a*a*b);
    }
    return maxv;
}

int main(){
    double xx,yy,zz;
    while(~scanf("%d",&n) && n){
        for(int i = 0; i < n; i++){
            scanf("%lf%lf%lf",&xx,&yy,&zz);
            p[i].x = eps+sqrt(xx*xx+yy*yy);
            p[i].y = zz;
        }
        double l = 0, r = PI/2, mid1, mid2;
        for(int i = 0; i < 100; i++){
            mid1 = l + (r - l)/3;
            mid2 = r - (r - l)/3;
            if(cal(mid1) < cal(mid2)) r = mid2;
            else l = mid1;
        }
        double v = cal(l);
        ansr = pow(v/tan(l),(double)(1.0/3));
        ansh = tan(l)*ansr;
        printf("%.3lf %.3lf\n",ansh,ansr);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章