HDU-2899(三分搜索)

Strange fuction

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


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
 
这题其实是2199的姊妹题,2199题为一单调函数,故只要用二分搜索查一下即可,研究此函数单调性,可知,该函数应为先减后增(求导即可知)、、故此题有两种解法,第一种是求出函数的导函数,很明显为一单调函数,则,对此导函数进行二分搜索,找到F为0的点,即为极值点,代入原函数即可。由于跟2199差不多,所以不重复劳动了
主要学习了一种新的方法,三分搜索,在某大神的博客里搜到了该算法的详解,三分主要用来解决非单调函数的求值,其时就是二分的升级版,
思想可以说完全一样,只是在搜索时,增加了一个Mid变量,使得可以在单调性不同的两段函数上进行搜索,哪个更接近理想值,就马上进行逼近。。。根据这种思想,其实完全还可以拓展出四分,五分的搜索,来解决单调性更复杂的函数。
 
在此再次膜拜下王玉斌大神。。。二分真是奇妙无穷。
 
 

#include <cstdio>
#include <cmath>

double calc(double x,double yy)
{
return 6*pow(x,7)+8*pow(x,6)+7*pow(x,3)+5*x*x-yy*x;
}

int main()
{
 int t;double y;
 scanf("%d",&t);
while (t--)
{
scanf("%lf",&y);
double mid,mmid,le=0,ri=100.0;
  while((ri-le)>1e-6)
  { 
   mid=(ri+le)/2;
   mmid=(mid+ri)/2;
   if (calc(mid,y)<=calc(mmid,y))
     ri=mmid;
   else le=mid;
   }
printf("%.4f\n",calc(mid,y));
}
return 0;
}

posted @ 2013-01-24 11:37  KRisen  阅读(333)  评论(0编辑  收藏  举报