計算幾何 ural 1753

題意:求book在下滑過程中,中心距書架左邊欄最遠距離。

思路:剛開始並沒有思路,只是簡單的推出:在最開始的時候,book的中心距書架左邊欄的距離(簡稱最左邊距)爲0,當book的中心與書架左邊欄頂部重合時,最左邊距也爲0.那麼最左邊距的最大值肯定在這之間產生。搜了別人的題解,瞭解到三分法。至於什麼是三分法,我也是剛瞭解,只知道,單峯函數三分法,函數在給定定義域內有不超過一個的峯值,可以根據其單峯特性來三分逼近答案。

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

using namespace std;

#define eps 1e-8

int main()
{
    double h, H, l;
    scanf("%lf %lf %lf", &h, &H, &l);
    H /= 2;
    double left, right, tmp, mid1, mid2, ans1, ans2;
    left = eps;
    tmp = sqrt(H*H - h*h);
    if (tmp <= l)
        right = tmp;
    else
        right = l;
    while (fabs(right - left) > eps) {
        mid1 = (left + right) / 2;
        mid2 = (left + mid1) / 2;
        ans1 = mid1*H/sqrt(mid1*mid1+h*h) - mid1;
        ans2 = mid2*H/sqrt(mid2*mid2+h*h) - mid2;
        if (ans1 > ans2)
            left = mid2;
        else
            right = mid1;
    }
    printf("%.6lf\n", ans1);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章