您的位置:首页 > 其它

HDU 2899 Strange fuction (二分)

2014-10-16 19:11 399 查看

Strange fuction

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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)

[align=left]Output[/align]
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.

[align=left]Sample Input[/align]

2
100
200

[align=left]Sample Output[/align]

-74.4291
-178.8534

[align=left]Author[/align]
Redow

[align=left]Recommend[/align]
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: