Introduction to the three-part method
in the interval with two mid to divide the interval into three parts, such a lookup algorithm called the three-point lookup, that is, the three-point method, the three-point method is often used to solve the maximum value of a single peak function.
There is also the understanding that, on the basis of a binary search, a second score is made on the left or right interval.
the difference between three points and two points
The two-point method is suitable for monotone functions, and the single peak function is not very good with two points, for some of the functions, can be derivative into a monotone function, so that the use of two points, however, many cases of derivation is very troublesome, then need to use three points.
Algorithm Introduction
1. First interval three points, each interval length is 1/3 (right-left)
mid1=left+ (Right-left)/3;
mid2=right-(Right-left)/3;
2. Compare Mid1 and Mid2 who is closer to the extreme value, if the MID1 is closer to the extreme value, the right interval to Mid2, otherwise the left interval to MID1 (the following code is to maximize the case)
if (Calc (mid1) <calc (MID2))
left=mid1;
else
Right=mid2;
3. Repeat the 1,2 process until the left+eps<right is not met, which means finding the maximum value
algorithm Templates
#define EPS 10e-6
double cal () {}//) The value required to compute the topic while
(l+eps<r)
{
m1=l+ (r-l)/3;
m2=r-(R-L)/3;
V1=cal (M1);
V2=cal (m2);
if (v1<v2) l=m1;
else r=m2;
}
Another way of writing
Double Calc (Type a)
{/
* is calculated according to the meaning of the topic */
}
void Solve (void)
{double left
;
Double mid, Midmid;
Double Mid_value, Midmid_value;
left = MIN; right = MAX;
while (left + EPS < right)
{
mid = (left + right)/2;
Midmid = (mid + right)/2;
Mid_area = Calc (mid);
Midmid_area = Calc (midmid);
if (Mid_area >= midmid_area) right = Midmid;
else left = mid;
}
}
Personally or like the first type of writing, feel the same size of the interval or more harmonious O (∩_∩) o~~
a few questions and three points
poj 3737 umbasketella
title: http://poj.org/problem?id=3737
The surface area of the vertebral body is given, the maximum volume and the height and the bottom radius of the time are obtained.
Puzzle : http://blog.csdn.net/caduca/article/details/43452139
HDU 2438 Turn the Corner
Title: http://acm.hdu.edu.cn/showproblem.php?pid=2438
Give the width of the x,y axis on the x-axis width y, and the long L and wide w of the car to determine whether the turn can be successful.
Puzzle : http://blog.csdn.net/caduca/article/details/43487677
HDU 3400 Line Belt
Topic http://acm.hdu.edu.cn/showproblem.php?pid=3400
The minimum time from point A to D is known for two line segments, knowing the speed of the AB,CD on both segments P,q and the speed r not on two segments.
Puzzle : http://blog.csdn.net/caduca/article/details/43484995