HDU 5105 Math Problem

让求  f(x)=|a∗x3+b∗x2+c∗x+d|(L≤x≤R)的最大值

这个题目讨论a和b的值,如果a==0的话,那么这个方程就变成了一个一元二次方程,直接找端点和对称轴(如果对称轴在给定的区间内)处的函数值就行,如果a != 0,那么求导,求导之后判断二次方程的delta,如果delta小于等于0,说明是单调的,那么最值还是端点处取到,如果delta大于0, 那么就要比较两个极点(如果极点在给定的区间内)处的值和端点值的大小就行了。

/*************************************************************************
    > File Name:            math.cpp
    > Author:               Howe_Young
    > Mail:                 [email protected]
    > Created Time:         2015年09月14日 星期一 20时18分44秒
 ************************************************************************/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>

using namespace std;
typedef long long ll;
double a, b, c, d, L, R;
const double eps = 1e-8;
double func(double x)
{
    return fabs(a * x * x * x + b * x * x + c * x + d);
}
int sgn(double x)
{
    if (fabs(x) < eps) return 0;
    return x > 0 ? 1 : -1;
}
int main()
{
    while (~scanf("%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &L, &R))
    {
        double ans = max(func(L), func(R));
        if (a == 0)
        {
            if (b != 0)
            {
                double t = -c / 2.0 / b;
                if (sgn(t - L) > 0 && sgn(t - R) < 0)
                    ans = max(ans, func(t));
            }
        }
        else
        {
            double delta = 4 * b * b - 12 * a * c;
            if (delta > 0)
            {
                double x1 = (-2.0 * b - sqrt(delta)) / 6.0 / a;
                double x2 = (-2.0 * b + sqrt(delta)) / 6.0 / a;
                if (sgn(x1 - L) >= 0 && sgn(x1 - R) <= 0)
                    ans = max(ans, func(x1));
                if (sgn(x2 - L) >= 0 && sgn(x2 - R) <= 0)
                    ans = max(ans, func(x2));
            }
        }
        printf("%.2lf\n", ans);
    }
    return 0;
}
时间: 2024-11-03 16:34:33

HDU 5105 Math Problem的相关文章

hdu 5105 Math Problem(数学)

题目链接:hdu 5105 Math Problem 题目大意:给定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

HDU 5105 Math Problem(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 Problem Description Here has an function: f(x)=|a?x3+b?x2+c?x+d|(L≤x≤R) Please figure out the maximum result of f(x). Input Multiple test cases(less than 100). For each test case, there will be only

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了.

hdu 6182A Math Problem(快速幂)

You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n. InputThere are no more than 50 test cases. Each case only contains a positivse integer n in a line. 1≤n≤10^18OutputFor each test case, output an integer i

hdu 5170 GTY&#39;s math problem(水,,数学,,)

题意: 给a,b,c,d. 比较a^b和c^d的大小 思路: 比较log(a^b)和log(c^d)的大小 代码: int a,b,c,d; int main(){ while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){ double x1 = b*log((double)a); double x2 = d*log((double)c); if(fabs(x1-x2)<eps){ puts("=")

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

HDU 5615 Jam&#39;s math problem

Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is trying to factorize ax^2+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m).He could only solve the problem in which p,q,m,k are positive numbers.Ple

BestCoder Round #70 Jam&#39;s math problem(hdu 5615)

Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ax^2+bx+cax?2??+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m)pqx?2??+(qk+mp)x+km=(px+k)(qx+m). He could only solve the problem in which p,q,m,

hdu 5615 Jam&#39;s math problem(十字相乘判定)

d. Jam有道数学题想向你请教一下,他刚刚学会因式分解比如说,x^2+6x+5=(x+1)(x+5) 就好像形如 ax^2+bx+c => pqx^2+(qk+mp)x+km=(px+k)(qx+m) 但是他很蠢,他只会做p,q,m,kp,q,m,k为正整数的题目 请你帮助他,问可不可以分解 题意就是问一个一元二次方程能不能进行十字相乘的分解? s. 官方题解:第一道题比较简单,可以说是简单的模拟题,我们考虑到a,b,c都是10^9??的,所以我们决定要把时间复杂度降下来, 对于每一个数,因为