Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2523 Accepted Submission(s): 606
f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R)
Please figure out the maximum result of f(x).
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const double eps = 1e-8; int main(){ double a,b,c,d,l,r; while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&l,&r)!=EOF){ double ans = 0; if(fabs(a)<eps){ ans = max(fabs(a*l*l*l+b*l*l+c*l+d),fabs(a*r*r*r+b*r*r+c*r+d)); if(fabs(b)>eps){ double mid = -c/(2*b); if(mid>=l&&mid<=r) ans = max(fabs(b*mid*mid+c*mid+d),ans); } }else{ ans = max(fabs(a*l*l*l+b*l*l+c*l+d),fabs(a*r*r*r+b*r*r+c*r+d)); if(4*b*b-12*a*c>0){ double delta = 4*b*b-12*a*c; double x1 = (-2*b-sqrt(delta))/(6*a); double x2 = (-2*b+sqrt(delta))/(6*a); if(x1>=l&&x1<=r) ans = max(ans,fabs(a*x1*x1*x1+b*x1*x1+c*x1+d)); if(x2>=l&&x2<=r) ans = max(ans,fabs(a*x2*x2*x2+b*x2*x2+c*x2+d)); } } printf("%.2lf\n",ans); } return 0; }