hdu 5105 Math Problem(数学)
HDU 数学 Problem Math
2023-09-14 09:06:25 时间
题目大意:给定a。b,c,d。l,r。表示有一个函数f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R),求函数最大值。
解题思路:考虑极点就可以,将函数求导后得到f′(x)=0的x,即为极值点。在极值点处函数的单调性会发生变化,所以最大值一定就在区间边界和极值点上。注意a=0。b=0的情况,以及极值点不在区间上。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-9;
double A, B, C, D, L, R;
double function(double x) {
if (x >= L && x <= R) {
double y = A * x * x * x + B * x * x + C * x + D;
return fabs(y);
}
return -1;
}
double solve (double a, double b, double c) {
if (fabs(a) < eps) {
if (fabs(b) < eps)
return -1;
else
return function(- c / b);
}
double dta = b * b - 4 * a * c;
if (dta > 0) {
dta = sqrt(dta);
return max(function( (-b + dta) / 2 / a ), function( (-b - dta) / 2 / a) );
}
return -1;
}
int main () {
while (~scanf("%lf%lf%lf%lf%lf%lf", &A, &B, &C, &D, &L, &R)) {
double ans = max(function(L), function(R));
ans = max(ans, solve(3*A, 2*B, C));
while (ans < 0);
printf("%.2lf\n", ans);
}
return 0;
}
相关文章
- HDU 4708 Rotation Lock Puzzle(模拟)
- HDU 3006 The Number of set(位运算 状态压缩)
- 【23.91%】【hdu 4694】Important Sisters("支NMLGB配树"后记)(支配树代码详解)
- 【hdu 2176】取(m堆)石子游戏
- 【hdu 1850】Being a Good Boy in Spring Festival
- HDU 2732 Leapin' Lizards(拆点+最大流)
- HDU 1796 How many integers can you find(容斥原理+二进制/DFS)
- HDU oj 开门人与关门人
- HDU 5071 Chat(2014鞍山B,模拟)
- hdu 4710 Balls Rearrangement (数学思维)
- Galaxy (hdu 5073 数学)
- HDU 3032 Nim or not Nim? (需求的游戏SG功能)