Reprint Address: http://blog.csdn.net/acdreamers/article/details/9989197
The first thing to say is the concept of three points:
The two points are divided into two segments of equal length, and three points are divided into three segments of equal length, to find, so that the search is called a three-point search, three points to find a pass
often used to quickly determine the maximum value.
As we all know, the requirements of the binary algorithm is that the search sequence is a monotone sequence, while the three-method-oriented search sequence requires: The sequence is a convex function.
similar to the dichotomy method, the three-part algorithm first divides the interval into three segments of equal length, then there are two points between L and R: ll=l+ (r-l)/3= (2L+R)/3 and
rr=r-(r-l)/3= (L+2R)/3
if LL is closer to the maximum than RR, we discard the right interval, otherwise we discard the left interval.
correctness of the algorithm:
1, LL and RR on the same side of the maximum value. Since the convexity function is monotonic on either side of the maximum (minimum), LL and RR are larger
The number of (small) is naturally closer to the maximum value. At this point, the interval that we are far from the most value cannot contain the maximum value, so we can discard it.
2, LL and RR on both sides of the maximum value. Since the maximum value is in the middle of an interval, after we discard a range, it does not affect the maximum value.
Typical topic: hdu4355,hdu2438,poj3301
title: party All the time
[CPP]View Plaincopy
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- Using namespace std;
- Const Double eps=1e-4;
- const int n=50010;
- Double P[n],w[n];
- int n;
- Double equ (double x)
- {
- double ans=0;
- For (int i=0;i<n;i++)
- {
- double S=fabs (p[i]-x);
- ans+=w[i]*s*s*s;
- }
- return ans;
- }
- Double Ternarysearch (double L,double R)
- {
- While (r-l>eps)
- {
- double ll= (2*l+r)/3;
- double rr= (l+2*r)/3;
- double ans1=equ (LL);
- double ans2=equ (RR);
- if (ans1>ans2)
- L=ll;
- Else
- R=RR;
- }
- return L;
- }
- int main ()
- {
- int t,i;
- scanf ("%d", &t);
- For (int t=1;t<=t;t++)
- {
- scanf ("%d", &n);
- double l=1000000;
- double r=-1000000;
- For (i=0;i<n;i++)
- {
- scanf ("%lf%lf", &p[i],&w[i]);
- if (p[i]<l) l=p[i];
- if (p[i]>r) r=p[i];
- }
- double Tmp=ternarysearch (l,r);
- printf ("Case #%d:%.0lf\n", t,equ (TMP));
- }
- return 0;
- }
title: Turn the corner
Test Instructions: Given the width of two roads at a right angle, then give the length and width of the car, and ask if the car can pass the curve?
such as:
to make the car turn this way, the left side of the car is as far as the right-angled point, and the car's bottom bottom is as far as the bottom side.
we establish a Cartesian coordinate system with the O point as the origin, and we can give a function f (a) of the P-point horizontal axis according to angle a .
then it's easy to get:
There are conditions:, it can be easily proved that a single-peak function, so next is the three points, if the maximum value is less than or equal to
y, then you can pass this right-angled corner, otherwise it won't work.
[CPP]View Plaincopy
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- Using namespace std;
- Const Double eps=1e-8;
- Double x,y,l,w;
- Double equ (double t)
- {
- return L*cos (t) + (W-x*cos (t))/sin (t);
- }
- Double Ternarysearch (double L,double R)
- {
- While (r-l>eps)
- {
- double ll= (2*l+r)/3;
- double rr= (l+2*r)/3;
- double ans1=equ (LL);
- double ans2=equ (RR);
- if (ans1<ans2)
- L=ll;
- Else
- R=RR;
- }
- return L;
- }
- int main ()
- {
- while (~scanf ("%lf%lf%lf%lf", &x,&y,&l,&w))
- {
- Double Low=0,high=acos (-1.0)/2;
- double Tmp=ternarysearch (Low,high);
- if (EQU (TMP) <=y) puts ("yes");
- else puts ("no");
- }
- return 0;
- }
title: Texas Trip
Test Instructions: The coordinates of a given n point on a plane, and the area of the smallest square containing the n points is calculated.
Analysis: We first find a small side length is parallel to the axis of the smallest square, and then we only need at 0-90 degrees all point rotation process, each rotation a little
Compare, and then record the edge length of the smallest square, and in this rotation process of the square side length about the rotation angle function is a single-peak function, so you can three points.
Here you know the coordinate rotation formula:
if the coordinates before the rotation are rotated, then there are:
[CPP]View Plaincopy
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- Using namespace std;
- Const Double eps=1e-12;
- Const Double Pi=acos (-1.0);
- Const Double inf=1e6;
- const int n=35;
- Double X[n],y[n];
- int n;
- Double Tri (double t)
- {
- double tx,ty;
- double Bx,by,sx,sy;
- Bx=by=-inf;
- Sx=sy=inf;
- For (int i=0; i<n; i++)
- {
- Tx=x[i]*cos (t)-y[i]*sin (t);
- Ty=x[i]*sin (t) +y[i]*cos (t);
- //Rotate the coordinate system to find out where the rotated point should be
- Bx=max (TX,BX);
- Sx=min (TX,SX);
- By=max (Ty,by);
- Sy=min (Ty,sy);
- }
- return Max (Bx-sx,by-sy);
- }
- Double Ternarysearch (double L,double R)
- {
- While (r-l>eps)
- {
- double ll= (2*l+r)/3;
- double rr= (l+2*r)/3;
- double Ans1=tri (LL);
- double Ans2=tri (RR);
- if (ans1>ans2)
- L=ll;
- Else
- R=RR;
- }
- return L;
- }
- int main ()
- {
- int t;
- scanf ("%d", &t);
- While (t--)
- {
- scanf ("%d", &n);
- For (int i=0;i<n;i++)
- scanf ("%lf%lf", &x[i],&y[i]);
- double LOW=0,HIGH=PI/2;
- double Tmp=ternarysearch (Low,high);
- printf ("%.2lf\n", Tri (TMP) *tri (TMP));
- }
- return 0;
- }
Three-point search algorithm (reprint learning) * "Template"