HDU 2899 Strange fuction (二分)

Strange fuction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3301    Accepted Submission(s): 2421


Problem Description
Now, here is a fuction:
  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
 

 

Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
 

 

Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
 

 

Sample Input
2 100 200
 

 

Sample Output
-74.4291 -178.8534
 

 

Author
Redow
 

 

Recommend
lcy
 
 
因为是求最小值,先求原函数的导数,
f '(x)=42x^6+48x^5+21x^2+10x-y
再次求导发现f(x)是一个单调递增函数。
当f '(0)>=0时,f(x)单调递增,最小值为f(0)。
当f '(100)<=0时,f(x)单调递减,最小值为f(100)。
当f '(0)<0 && f '(100)>0时,存在一个x,使得f '(x)=0。此时函数先递减再递增,所以最小值是f(x)。
 
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
using namespace std;
double num(double x,double y)
{
    return 6*pow(x,7.0)+8*pow(x,6.0)+7*pow(x,3.0)+5*pow(x,2.0)-y*x;
}
double fun(double x,double y)
{
    return 42*pow(x,6.0)+48*pow(x,5.0)+21*pow(x,2.0)+10*x-y;
}
int main()
{
    int T;
    double Y;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf",&Y);
        if(fun(0.0,Y)>=0) printf("%.4lf\n",num(0.0,Y));
        else if(fun(100.0,Y)<=0) printf("%.4lf\n",num(100.0,Y));
        else
        {
            double l=0.0,mid=50.0,r=100.0;
            double ans1,ans2,ans3;
            ans1=fun(l,Y);
            ans2=fun(mid,Y);
            ans3=fun(r,Y);
            while(fabs(ans1-ans2)>0.000001)
            {
                if(ans2>0)
                {
                    ans3=ans2;
                    r=mid;
                    mid=(l+r)/2;
                    ans2=fun(mid,Y);
                }
                else
                {
                    ans1=ans2;
                    l=mid;
                    mid=(l+r)/2;
                    ans2=fun(mid,Y);
                }
            }
            printf("%.4lf\n",num(mid,Y));
        }
    }
}
View Code

 

posted @ 2014-10-16 19:11  Cliff Chen  阅读(252)  评论(0编辑  收藏  举报