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;
}


你可能感兴趣的:(HDU 2899-Strange fuction)