首页 > 代码库 > HDU 5105 Math Problem --数学,求导

HDU 5105 Math Problem --数学,求导

官方题解:

f(x)=|a∗x3+b∗x2+c∗x+d|, 求最大值。令g(x)=a∗x3+b∗x2+c∗x+d,f(x)的最大值即为g(x)的正最大值,或者是负最小值。a!=0时,

g′(x)=3∗a∗x2+2∗b∗x+c 求出g′(x)的根(若存在,x1,x2,由导数的性质知零点处有极值。ans=max(f(xi)|L≤xi≤R).然后考虑两个端点的特殊性有ans=max(ans,f(L),f(R)).

当时 x = -c/(2*b) 写成 x = -c/2*b 了,然后过pretest了。 然后。。你敢信?

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#define eps 1e-8using namespace std;#define N 50017int sgn(double x){    if(x > eps) return 1;    if(x < -eps) return -1;    return 0;}double a,b,c,d,L,R;double calc(double x) { return fabs(a*x*x*x + b*x*x + c*x + d); }int main(){    while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R)!=EOF)    {        if(sgn(a) == 0)        {            if(sgn(b) == 0)            {                if(sgn(fabs(calc(L))-fabs(calc(R))) >= 0)                    printf("%.2f\n",calc(L));                else                    printf("%.2f\n",calc(R));            }            else            {                double X = -c/(2.0*b);                double k1 = calc(L);                double k2 = calc(R);                double k3;                if(sgn(X-L) >= 0 && sgn(X-R) <= 0)                    k3 = calc(X);                else                    k3 = 0.0;                printf("%.2f\n",max(max(k1,k2),k3));            }            continue;        }        double delta = 4.0*b*b - 12.0*a*c;        if(sgn(delta) <= 0)        {            if(sgn(fabs(calc(L))-fabs(calc(R))) >= 0)                printf("%.2f\n",calc(L));            else                printf("%.2f\n",calc(R));        }        else        {            double X1 = (-2.0*b + sqrt(delta))/(6.0*a);            double X2 = (-2.0*b - sqrt(delta))/(6.0*a);            double k1 = calc(L);            double k2 = calc(R);            double k3,k4;            if(sgn(X1-L) >= 0 && sgn(X1-R) <= 0)                k3 = calc(X1);            else                k3 = 0.0;            if(sgn(X2-L) >= 0 && sgn(X2-R) <= 0)                k4 = calc(X2);            else                k4 = 0.0;            printf("%.2f\n",max(max(max(k1,k2),k3),k4));        }    }    return 0;}
View Code

 

HDU 5105 Math Problem --数学,求导