码迷,mamicode.com
首页 > 其他好文 > 详细

三分法搜索

时间:2018-07-18 23:32:25      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:iss   fir   ble   实现   lin   pre   gravity   length   rate   

二分法适用于求单调的时候用的,就比如说排序好的数组,那是递增的或者递减的。如果像出现了非单调函数那样的怎么求它的最值呢?

 二分法早就失去了他的意义了。不过还是可以用三分法来实现的,就是二分中再来二分。三分查找的算法,对于求凸性或凹性函数的极值非常方便

技术分享图片

如图所示,已知左右端点L、R,要求找到极值点的位置。 
思路:通过不断缩小 [L,R] 的范围,无限逼近极值点。 
做法:先取 [L,R] 的中点 mid,再取 [mid,R] 的中点 mmid,通过比较 f(mid) 与 f(mmid) 的大小来缩小范围。 
当最后 L=R-1 时,再比较下这两个点的值,我们就找到了答案。 
1、当 f(mid) > f(mmid) 的时候,我们可以断定 mmid 一定在极值点的右边。 

反证法:假设 mmid 在极值点的左边,则 mid 也一定在极值点的左边,又由 f(mid) > f(mmid) 可推出 mmid < mid,与已知矛盾,故假设不成立。 

所以,此时可以将 R = mmid 来缩小范围。 
2、当 f(mid) < f(mmid) 的时候,我们可以断定 mid 一定在极值点的左边。 

反证法:假设 mid 在极值点的右边,则 mmid 也一定在极值点的右边,又由 f(mid) < f(mmid) 可推出 mid > mmid,与已知矛盾,故假设不成立。 

同理,此时可以将 L = mid 来缩小范围。

 

 摘自https://blog.csdn.net/littlewhite520/article/details/70144763

 

第一种写法

 1 double  solve(double l,double r)
 2 {
 3     double mid,midmid;
 4     while(r - l > eps)
 5     {
 6         mid = (l + r)/2.0;
 7         midmid = (r + mid)/2.0;
 8         if(f(mid) <= f(midmid))// f 就算函数值
 9             r = midmid;
10         else  l = mid;
11     }
12     return f(l);
13 }


第二种写法

 1 double three_devide(double low,double up)  
 2 {  
 3     double m1,m2;  
 4     while(up-low>=eps)  
 5     {  
 6         m1=low+(up-low)/3;  
 7         m2=up-(up-low)/3;  
 8         if(f(m1)<=f(m2))  
 9             low=m1;  
10         else  
11             up=m2;  
12     }  
13     return (m1+m2)/2;  
14 }  

 

 

 Light Bulb

 

Compared to wildleopard‘s wealthiness, his brother mildleopard is rather poor. His house is narrow and he has only one light bulb in his house. Every night, he is wandering in his incommodious house, thinking of how to earn more money. One day, he found that the length of his shadow was changing from time to time while walking between the light bulb and the wall of his house. A sudden thought ran through his mind and he wanted to know the maximum length of his shadow.

技术分享图片

Input

 The first line of the input contains an integer T (T <= 100), indicating the number of cases.Each test case contains three real numbers H, h and D in one line. H is the height of the light bulb while h is the height of mildleopard. D is distance between the light bulb and the wall. All numbers are in range from 10-2 to 103, both inclusive, and H - h >= 10-2.

 

Output

 For each test case, output the maximum length of mildleopard‘s shadow in one line, accurate up to three decimal places..

 

Sample Input

3

2 1 0.5

2 0.5 3

4 3 4

 

Sample Output

1.000

0.750

4.000

三分法搜索

标签:iss   fir   ble   实现   lin   pre   gravity   length   rate   

原文地址:https://www.cnblogs.com/wkfvawl/p/9332790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!