首页 > ACM题库 > HDU-杭电 > HDU 2899-Strange fuction-分治-[解题报告]HOJ
2014
02-17

HDU 2899-Strange fuction-分治-[解题报告]HOJ

Strange fuction

问题描述 :

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.

输入:

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)

输出:

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)

样例输入:

2
100
200

样例输出:

-74.4291
-178.8534

       题目意思,求函数的最小值。

       很少见这种要高数方法解的题目,我一直以为要有三分法,但极值毕竟不是最值,看了题解才知道解法,这里就引用牛人的解法吧:用到一次求导求单调性,二次求导判断凹凸性,然后二分查找求“极值”(个人认为是最值)

 

             以下是代码:

 

       

#include<cstdio>
#include<cstring>
#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>
using namespace std;
const double eps=1e-8;
int y;

double g(double x)
{
    return 42*pow(x,6)+48*pow(x,5)+21*pow(x,2)+10*x;
}

double f(double x)
{
    return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*pow(x,2)-y*x;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&y);
        if(g(100.0)-y<=0)
        {
            printf("%.4lf\n",f(100.0));
            continue;
        }
        double l=0,r=100,m;
        while(l+eps<=r)
        {
            m=(l+r)/2;
            if(g(m)-y<0)
                l=m;
            else
                r=m;
        }
        printf("%.4lf\n",f(m));
    }
    return 0;
}

 

解题参考:http://blog.csdn.net/new_c_yuer/article/details/6621453


  1. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。

  2. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。