HDU 2899-Strange fuction

凹函數求極小值,三分。

代碼如下:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
double fx(double x, double y)
{
    return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}
int main()
{
#ifdef test
    freopen("input.txt", "r", stdin);
#endif
    int t;
    double y;
    scanf("%d", &t);
    while(t--)
    {
        double l = 0, r = 100;
        scanf("%lf", &y);
        while(l + 1e-7<= r)
        {
            double mid = (l + r) / 2;
            double midmid = (mid + r) / 2;
            double ans_mid = fx(mid, y), ans_midmid = fx(midmid, y);
            if(ans_mid <=  ans_midmid)
                r = midmid;
            else
                l = mid;
        }
        printf("%.4lf\n", fx((l+r)/2, y));
    }
    return 0;
}


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