f(x)=|ax3+bx2+cx+d|

求f(x)在L≤x≤R的最大值。

分析

参数有可能是0,注意分类讨论

1.当a=0时

  b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间端点。

  b≠0,f为二次函数,最大值点在区间端点或者x0=c/(2*b),当L≤x0≤R时,ans=max{f(L),f(R),f(x0)}。

2.当a≠0时,f为三次函数

  最大值点在区间端点或者导函数的零点x1,x2

  注意x1,x2是否在[L,R]区间。

代码

#include<cstdio>
#include<algorithm>
#include<cmath>
#define dd double using namespace std; dd a,b,c,d,l,r;
dd f(dd x)
{
return fabs(a*x*x*x+b*x*x+c*x+d);
}
void ff(dd a,dd b,dd c,dd& ans)
{
if(a==)
{
if(b==) return;
ans=max(ans,f(-c/b));
return;
}
if(b*b<*a*c) return;
dd q=sqrt(b*b-*a*c);
dd x1=(-q-b)/(*a);
dd x2=(q-b)/(*a);
if(l<x1&&x1<r)
{
ans=max(ans,f(x1));
if(r>x2) ans=max(ans,f(x2));
}
else if(l<x2&&x2<r) ans=max(ans,f(x2));
}
int main()
{
while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&l,&r))
{
dd ans;
ans=max(f(l),f(r));
ff(*a,*b,c,ans);
printf("%.2lf\n",ans);
}
return ;
}

下面这样写,省了判断区间和顶点的不同位置关系。

#include<cstdio>
#include<algorithm>
#include<cmath>
#define dd double using namespace std; dd a,b,c,d,l,r,ans;
dd f(dd x)
{
if(x<l||x>r)return -;
return fabs(a*x*x*x+b*x*x+c*x+d);
}
dd ff(dd a,dd b,dd c)
{
if(a==)
{
if(b==) return -;
return f(-c/b);
}
if(b*b<*a*c) return -;
dd q=sqrt(b*b-*a*c);
dd x1=(-q-b)/a;
dd x2=(q-b)/a;
return max(f(x1/),f(x2/));
}
int main()
{
while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&l,&r))
{
ans=max(f(l),f(r));
ans=max(ans,ff(*a,*b,c));
printf("%.2lf\n",ans);
}
return ;
}

【HDU 5105】Math Problem的更多相关文章

  1. 【HDU 5858】Hard problem

    边长是L的正方形,然后两个半径为L的圆弧和中间半径为L的圆相交.求阴影部分面积. 以中间圆心为原点,对角线为xy轴建立直角坐标系. 然后可以联立方程解出交点. 交点是$(\frac{\sqrt{7} ...

  2. 【HDU 5858】Hard problem(圆部分面积)

    边长是L的正方形,然后两个半径为L的圆弧和中间直径为L的圆相交.求阴影部分面积. 以中间圆心为原点,对角线为xy轴建立直角坐标系. 然后可以联立方程解出交点. 交点是$(\frac{\sqrt{7} ...

  3. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  4. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  5. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  6. [HDU - 5170GTY's math problem 数的精度类

    题目链接:HDU - 5170GTY's math problem 题目描述 Description GTY is a GodBull who will get an Au in NOI . To h ...

  7. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  8. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  9. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

随机推荐

  1. Windows Server 2008 DNS服务器安装与配置

    Windows Server 2008 DNS服务器安装与配置本文关键字:Windows Server 2008 DNS.IIS 服务器安装与配置,DNS服务器本文来源:http://www.ip-t ...

  2. HTML5 web workes实现多线程

    对多线程来说尽量使用HTML5的WEB WORKER特性 HTML5中的Web Worker是使用多个线程并发执行Javascript程序.另外,这种特别的多线程实现能减少困惑开发者多年的,在其他平台 ...

  3. f2fs解析(一)f2fs如何解决wandering tree

    wandering tree问题是log-structured 文件系统(LFS) 特有的一个问题,因为LFS的脏数据是追加更新的,所以如果一个数据块变脏了,那么那个数据块的直接索引块.间接索引块都会 ...

  4. [3]MVC3中使用Telerik(转)

    ASP.NET MVC的Model元数据与Model模板:预定义模板 http://www.cnblogs.com/artech/archive/2012/05/02/model-metadata-a ...

  5. android camera setParameters failed 类问题分析总结

    在 monkey test 测试中出现了一例 RuntimeException ,即 setParameters failed. LOG显示为:09-01 18:47:17.348 15656 156 ...

  6. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. Volly框架的使用基础版及使用中的一些坑 Ace 网络篇(三)

    直接把注释粘过来: * Volley使用讲解: * 要实现网络数据请求主要要记住下面三步骤: * 1.创建RequestQueue对象 * 2.创建XXRequest对象(XX代表String,JSO ...

  8. [CareerCup] 3.7 Adopt Animal 领养动物

    3.7 An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out ...

  9. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  10. 20135220谈愈敏Blog7_可执行程序的装载

    可执行程序的装载 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029000 一. ...