Problem 1056 - 函数问题 ---三分法

来源:互联网 发布:繁体字转换软件 编辑:程序博客网 时间:2024/05/18 12:43
Problem 1056 - 函数问题
Time Limit: 1000MS      Memory Limit: 65536KB     Difficulty:    
Total Submit: 264     Accepted: 44     Special Judge: No 

Description

本地图片,请重新上传本地图片,请重新上传

我们定义函数Y=Ax^2+Bx+C;  (0<=A<=100,0<=|B|<=5000,|C|<=5000)
现在有两个这样的函数Y1,Y2;
我们定义新的函数F(x); F(x)在x=x0处的函数值为  F(x0)=max(Y1(x=x0),Y2(x=x0));
现在告诉你这两个函数的A,B,C ,输出0<=x0<=1000 之间最小的F(x0):

Input

多组数据,第一行为Case 数 T   (T<=100)
每个Case 下有两行,每行三个整数,分别为每个函数的A,B,C;
(0<=A<=100,0<=|B|<=5000,|C|<=5000)

Output

对于每个Case,输出一个实数,保留四位小数

Sample Input

2
2 0 0
2 0 0
2 0 0
2 -4 2

Sample Output

0.0000
0.5000

开始一看,就是个水三分,可是细节没弄好。。结果惨痛。。。唉,没事,第一次写三分,以后注意就行。

三分时利用区间关系和设定阀值的大小关系作比较觉得不如直接迭代来的保险,不然精度把握不好会WA死。。。

 

#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm> #define eps 1e-10using namespace std; const int maxn=3;int a[maxn],b[maxn],c[maxn]; double F(double x){    double ans=-111111111;//由于数据中结果可能有负值,所以此处应把ans初值设为更小的负值;    for(int i=1;i<=2;i++)    ans=max(ans,a[i]*x*x+b[i]*x+c[i]);    return ans;} int main(){    int test;    scanf("%d",&test);    while(test--)    {        for(int i=1;i<=2;i++)        {            scanf("%d%d%d",&a[i],&b[i],&c[i]);        }        double low=0.0,high=1000.0;        for(int i=0;i<100;i++)//此处可以直接迭代;        {            double m1=low+(high-low)/3;            double m2=high-(high-low)/3;            if(F(m1)<F(m2))                high=m2;            else                low=m1;        }        printf("%.4lf\n",F(low));    }    return 0;}


 

原创粉丝点击