三分查找算法(转载学习)*【模板】

转载地址:http://blog.csdn.net/acdreamers/article/details/9989197

首先来说说三分的概念:

二分是把区间分为长度相等的两段,三分则是把区间分为长度相等的三段,进行查找,这样的查找称为三分查找,三分查找通
 
常用来迅速确定最值。
 
众所周知,二分算法的要求是搜索的序列是单调序列,而三分法所面向的搜索序列的要求是:序列为一个凸性函数。
 
 
 
与二分法类似,三分算法先把区间分为长度相等的三段,那么l与r之间就有两个点,分别是:ll=l+(r-l)/3=(2l+r)/3和
 rr=r-(r-l)/3=(l+2r)/3
 
如果ll比rr更靠近最值,我们就舍弃右区间,否则我们舍弃左区间。
 
 
算法的正确性:
 
1、ll与rr在最值的同一侧。由于凸性函数在最大值(最小值)任意一侧都具有单调性,因此,ll与rr中,更大
(小)的那个数自然更为靠近最值。此时,我们远离最值的那个区间不可能包含最值,因此可以舍弃。
2、ll与rr在最值的两侧。由于最值在中间的一个区间,因此我们舍弃一个区间后,并不会影响到最值。
 
 
典型题目:HDU4355,HDU2438,POJ3301
 
 
 
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4. #include <math.h>  
  5.   
  6. using namespace std;  
  7.   
  8. const double EPS=1e-4;  
  9. const int N=50010;  
  10.   
  11. double p[N],w[N];  
  12. int n;  
  13.   
  14. double equ(double x)  
  15. {  
  16.     double ans=0;  
  17.     for(int i=0;i<n;i++)  
  18.     {  
  19.         double S=fabs(p[i]-x);  
  20.         ans+=w[i]*S*S*S;  
  21.     }  
  22.     return ans;  
  23. }  
  24.   
  25. double ternarySearch(double l,double r)  
  26. {  
  27.     while(r-l>EPS)  
  28.     {  
  29.         double ll=(2*l+r)/3;  
  30.         double rr=(l+2*r)/3;  
  31.         double ans1=equ(ll);  
  32.         double ans2=equ(rr);  
  33.         if(ans1>ans2)  
  34.             l=ll;  
  35.         else  
  36.             r=rr;  
  37.     }  
  38.     return l;  
  39. }  
  40.   
  41. int main()  
  42. {  
  43.     int T,i;  
  44.     scanf("%d",&T);  
  45.     for(int t=1;t<=T;t++)  
  46.     {  
  47.         scanf("%d",&n);  
  48.         double l=1000000;  
  49.         double r=-1000000;  
  50.         for(i=0;i<n;i++)  
  51.         {  
  52.            scanf("%lf%lf",&p[i],&w[i]);  
  53.            if(p[i]<l) l=p[i];  
  54.            if(p[i]>r) r=p[i];  
  55.         }  
  56.         double tmp=ternarySearch(l,r);  
  57.         printf("Case #%d: %.0lf ",t,equ(tmp));  
  58.     }  
  59.     return 0;  
  60. }  
 
题意:给定一个直角弯道的两条道路的宽度,然后再给出汽车的长度与宽度,问汽车能否通过该弯道?
 
如下图:
 
要使汽车能转过此弯道,那么就是汽车的左边尽量贴着那个直角点,而汽车的右下后方的点尽量贴着最下面的边。
我们以O点为原点建立直角坐标系,我们可以根据角a给出P点横坐标的函数F(a)
 
那么很容易得到:
 
其中有条件:,可以很容易证明是一个单峰函数,所以接下来就是三分了,如果的最大值小于等于
 
y,那么就能通过此直角弯道,否则就通不过。
 
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4. #include <math.h>  
  5.   
  6. using namespace std;  
  7.   
  8. const double EPS=1e-8;  
  9. double x,y,l,w;  
  10.   
  11. double equ(double t)  
  12. {  
  13.     return l*cos(t)+(w-x*cos(t))/sin(t);  
  14. }  
  15.   
  16. double ternarySearch(double l,double r)  
  17. {  
  18.     while(r-l>EPS)  
  19.     {  
  20.         double ll=(2*l+r)/3;  
  21.         double rr=(l+2*r)/3;  
  22.         double ans1=equ(ll);  
  23.         double ans2=equ(rr);  
  24.         if(ans1<ans2)  
  25.             l=ll;  
  26.         else  
  27.             r=rr;  
  28.     }  
  29.     return l;  
  30. }  
  31.   
  32. int main()  
  33. {  
  34.     while(~scanf("%lf%lf%lf%lf",&x,&y,&l,&w))  
  35.     {  
  36.         double low=0,high=acos(-1.0)/2;  
  37.         double tmp=ternarySearch(low,high);  
  38.         if(equ(tmp)<=y) puts("yes");  
  39.         else            puts("no");  
  40.     }  
  41.     return 0;  
  42. }  

 
 
题意:平面上给定n个点的坐标,求出包含这n个点的最小的正方形的面积。
 
分析:我们先找出一个边长都平行于坐标轴的最小的正方形,然后我们只需要在0到90度的所有点旋转过程中,每次旋转一点就
比较,然后记录边长最小的正方形,而在这个旋转过程中正方形的边长关于旋转角度的函数是一个单峰函数,所以可以三分。
 
这里要知道坐标旋转公式:
如果为旋转前的坐标,为旋转后的坐标,那么有:
 
 
 
    1. #include <iostream>  
    2. #include <string.h>  
    3. #include <stdio.h>  
    4. #include <math.h>  
    5.   
    6. using namespace std;  
    7.   
    8. const double EPS=1e-12;  
    9. const double PI=acos(-1.0);  
    10. const double INF=1e6;  
    11. const int N=35;  
    12.   
    13. double x[N],y[N];  
    14. int n;  
    15.   
    16. double Tri(double t)  
    17. {  
    18.     double tx,ty;  
    19.     double bx,by,sx,sy;  
    20.     bx=by=-INF;  
    21.     sx=sy=INF;  
    22.     for(int i=0; i<n; i++)  
    23.     {  
    24.         tx=x[i]*cos(t)-y[i]*sin(t);  
    25.         ty=x[i]*sin(t)+y[i]*cos(t);  
    26.         //旋转坐标系,求出旋转后的点应该在什么位置  
    27.         bx=max(tx,bx);  
    28.         sx=min(tx,sx);  
    29.         by=max(ty,by);  
    30.         sy=min(ty,sy);  
    31.     }  
    32.     return max(bx-sx,by-sy);  
    33.   
    34. }  
    35.   
    36. double ternarySearch(double l,double r)  
    37. {  
    38.     while(r-l>EPS)  
    39.     {  
    40.         double ll=(2*l+r)/3;  
    41.         double rr=(l+2*r)/3;  
    42.         double ans1=Tri(ll);  
    43.         double ans2=Tri(rr);  
    44.         if(ans1>ans2)  
    45.             l=ll;  
    46.         else  
    47.             r=rr;  
    48.     }  
    49.     return l;  
    50. }  
    51.   
    52. int main()  
    53. {  
    54.     int t;  
    55.     scanf("%d",&t);  
    56.     while(t--)  
    57.     {  
    58.         scanf("%d",&n);  
    59.         for(int i=0;i<n;i++)  
    60.            scanf("%lf%lf",&x[i],&y[i]);  
    61.         double low=0,high=PI/2;  
    62.         double tmp=ternarySearch(low,high);  
    63.         printf("%.2lf ",Tri(tmp)*Tri(tmp));  
    64.     }  
    65.     return 0;  
原文地址:https://www.cnblogs.com/yspworld/p/4270950.html