Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5105
Problem Descriptionhere have an function:
F (x)=|a? x 3 +b? x 2 +C?x+d |(L≤x≤R)
Please figure out the maximum result of f (x). Inputmultiple test Cases (less than 100). For each test case, there'll be is only 1 line contains 6 numbers A, B, C, D, L and R. (?Ten≤a,b,C,d ≤Ten,? -≤L≤R≤ -) Outputfor each test case, print the answer, is rounded to 2 digits after decimal point in 1 line. Sample Input
1.00 2.00 3.00 4.00 5.00 6.00
Sample Output
310.00
Sourcebestcoder Round #18
Ps:
Points a equals zero and a is not equal to zero in the case of the derivation of the discussion!
The code is as follows:
#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> #include < Cstring>using namespace Std;int main () {Double A, B, C, D, L, R; while (~SCANF ("%lf%lf%lf%lf%lf%lf", &a,&b,&c,&d,&l,&r)) {Double ansl = 0, AnsR = 0; Double Maxans = 0; if (A! = 0) {Double t = 4*b*b-12*a*c; Ansl = A*l*l*l+b*l*l+c*l+d; if (Ansl < 0) Ansl =-ansl; AnsR = A*r*r*r+b*r*r+c*r+d; if (AnsR < 0) AnsR =-ANSR; Maxans = max (ANSL,ANSR); if (t >= 0) {Double x1 = ( -2*b+sqrt (t))/(6*A); Double T1 = a*x1*x1*x1+b*x1*x1+c*x1+d; if (T1 < 0) T1 =-t1; Maxans = max (MAXANS,T1); Double x2 = ( -2*b-sqrt (t))/(6*A); Double t2 = a*x2*x2*x2+b*x2*x2+c*x2+d; if (T2 < 0) t2 =-t2; Maxans = max (MAXANS,T2); }} else if (a = = 0) {if (b! = 0) {Double TT =-c/(2*b); Double T1 = a*tt*tt*tt+b*tt*tt+c*tt+d; if (T1 < 0) T1 =-t1; Maxans = T1; } double t1 = a*l*l*l+b*l*l+c*l+d; if (T1 < 0) T1 =-t1; Maxans = max (MAXANS,T1); Double t2 = a*r*r*r+b*r*r+c*r+d; if (T2 < 0) t2 =-t2; Maxans = max (MAXANS,T2); } printf ("%.2lf\n", Maxans); } return 0;}
HDU 5105 Math problem (math)