您的位置:首页 > 其它

hdu 2899 Strange Fuction(二分)

2012-02-14 23:21 369 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2899 简单二分,但这个不满足单调性。

题目要求极值点,以函数导数的正负作为条件二分即可。

code:

#include<cstdio>
#include<cmath>
double y = 0 ;
double cal(double x){
return 42*pow(x, 6.0) + 48*pow(x, 5.0) + 21*pow(x, 2.0) + 10*x - y ;
}
double solve(double x){
return 6*pow(x, 7.0) + 8*pow(x, 6.0) + 7*pow(x, 3.0) + 5*pow(x, 2.0) - y*x ;
}
int main(){
int t ;
double r, l, m ;
scanf("%d", &t) ;
while(t--){
scanf("%lf", &y) ;
if(cal(100)<=0){ //x=100时若导数小于0,则函数满足单调递减
//因为42*pow(x, 6.0) + 48*pow(x, 5.0) + 21*pow(x, 2.0) + 10*x递增,x=100时有最大值
//不过官方数据好像没有用到这个剪枝
printf("%.4lf\n", solve(100)) ;
continue ;
}
l = 0 ;
r = 100 ;
while(r-l>1e-8){
m = (r + l) / 2 ;
double ans = cal(m) ;
if(ans<0)
l = m ;
else
r = m ;
}
printf("%.4lf\n", solve(m)) ;
}
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: