Three-point Search

Source: Internet
Author: User

We all know that binary lookup is suitable for the approximation of a value in a monotone function to solve a point.

If you encounter a convex or concave function, you can find the convex or concave point in three points.

The following method should be a variant of the three-point lookup.

, the left and right endpoint L, R, are required to find the location of the white point.

Idea: The white point is infinitely approximated by shrinking the [L,r] range.

Practice: First Take [L,r] midpoint Mid, then take [mid,r] midpoint mmid, by comparing F (mid) and F (mmid) size to narrow the range.

When the last l=r-1, compare the values of these two points, we find the answer.

1, when F (mid) > F (mmid), we can conclude that mmid must be on the right side of the white point.

Mmid: Assuming that the left side of the white point, mid also must be on the left side of the white point, and by F (mid) > F (mmid) can be introduced Mmid < Mid, with known contradictions, it is assumed that is not established.

Therefore, you can narrow the range by R = Mmid.

2, when F (mid) < F (mmid), we can conclude that the mid must be on the left side of the white point.

To disprove: If the mid is on the right side of the white point, then Mmid must also be on the right side of the white point, and by F (mid) < F (Mmid) can launch mid > Mmid, with known contradictions, it is assumed not to be established.

In the same vein, L = Mid can be reduced by this time.

1 intSanfen (intLintR//find the bump.2 {  3      while(L < R1)  4     {  5         intMid = (l+r)/2; 6         intMmid = (mid+r)/2; 7         if(f (Mid) >f (mmid))8R =Mmid; 9         Else  TenL =mid;  One     }   A     returnF (L) > F (r)?l:r;  -}
First, the concept on the basis of binary search, in the right interval (or left interval) and then another two points, such a search algorithm is called three-point search, that is, three-point method. A three-point lookup is often used to quickly determine the maximum value. The requirement for a binary search sequence to be searched for is monotonic (not necessarily strictly monotonous); a sequence with no monotonicity is not a binary lookup. Unlike binary lookups, the requirement for a search sequence that is oriented by a three-part method is that the sequence is a convex function. In layman's terms, the sequence must have a maximum (or minimum) value at the left of the maximum (minimum value) sequence, which must satisfy the non-strict monotonically incrementing (decrement), and the right sequence must meet the non-strict monotonically decreasing (incrementing). For example, a convex function with a maximum value: Second, the algorithm process (1), similar to the dichotomy method, first take the middle value of the entire interval mid.
    1. Mid = (left + right)/2;
(2), and then take the middle value of the right interval midmid, thus dividing the interval into three small intervals.
    1. Midmid = (mid + right)/2;
(3), our mid is closer than Midmid, we abandon the right zone, or we abandon the left interval? To compare mid with Midmid who is closest to the maximum value, just determine the size of the function value where mid is located and the function value where Midmid resides. When the maximum value is Max, the larger of mid and Midmid is closer to the highest value. The same as the minimum value. 1 if (Cal (mid) > cal (midmid)) 2 right = midmid; 3 Else 4 left = mid; (4), repeat (1) (2) (3) until the maximum value is found.
(5), another three-point notationdouble Three_devide (double Low,double up)
1 {  2     Doublem1,m2; 3      while(up-low>=EPS)4     {  5m1=low+ (Up-low)/3; 6m2=up-(Up-low)/3; 7         if(f (M1) <=f (m2))8low=M1; 9         Else  Tenup=m2;  One     }   A     return(M1+M2)/2;  -}

       
The correctness of the algorithm: 1, mid and midmid on the same side of the most value. Since the convexity function is monotonic on either side of the maximum (minimum), the larger (small) number in mid and midmid 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, Mid and midmid on both sides of the most value. Because the most value is in the middle of an interval, so we discard a range, and does not affect the maximum
1 Const DoubleEPS = 1e-Ten; 2   3 DoubleCalcDoublex)4 {  5     //f (x) =-(x-3) ^2 + 2; 6     return-(X-3.0) * (X-3.0) +2; 7 }  8   9 DoubleTernarysearch (DoubleLowDoubleHigh )Ten {   One     DoubleMid, Midmid;  A      while(Low + EPS <High ) -     {   -Mid = (low + high)/2;  theMidmid = (mid + high)/2;  -         DoubleMid_value =Calc (mid);  -         DoubleMidmid_value =Calc (midmid);  -         if(Mid_value >midmid_value) +High =Midmid;  -         Else   +Low =mid;  A     }   at     returnLow ;  -}


Call Ternarysearch (0, 6) and return a result of 3.0000

As the most common method in the division, the dichotomy method is applicable to the monotone function and approximate to the value of a certain point. However, when the function is a convex function, the dichotomy can not be applied, then the three-part method will be able to "work" ~ ~


      , similar to the definition of two left and Right,mid = (left + right)/2,midmid = (mid + right)/2; If mid is near the extremum point, right = midmid; otherwise (i.e. Midmid is near the extreme point), then left = mid; The

Program template is as follows:

Double Calc (Type a)
{
    * based on the meaning of the topic */
}

void Solve (void)
{
Double left, right;
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);
Assume the maximum extremum is solved.
if (Mid_area >= midmid_area) right = Midmid;
else left = mid;
}
}

According to several OJ, the concrete realization of the three-part method is analyzed.

BUAA 1033 Easy Problem
http://acm.buaa.edu.cn/oj/problem_show.php?c=0&p=1033

Test instructions to find a point on a line segment, the distance from the given P-point is minimal. It is obvious that the convex function is solved by the method of three points.
The Calc function is the distance from a point to a P point.

ZOJ 3203 Light Bulb
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203


, people move around, seeking the longest length of the shadow L.
According to the figure, it is easy to find that when the lamp, the person's head and corner into a straight line (assuming at this time the person standing at a point), the length of this time is the shadow all on the ground the longest length. When people go to the right, the shadow begins to be projected onto the wall, and when the person is affixed to the wall, the shadow length is the height of the human being. So when people walk from point A to the wall, the function is incremented and then decremented, which is the convex function, so we can solve it by the method of three.

Only the Calc function is given below, and the other direct templates are available.
Double Calc (Double x)
{
Return (H * d-h * x)/(d-x) + x;
}

Heru 5081 Turn the corner 08 Harbin Regional online Game
http://acm.hrbeu.edu.cn/index.php?act=problem&id=1280


Car turn problem, given X, Y, L, D to determine whether to turn. First, if x or Y is less than D, then it must not.
Secondly, we find that with the increase of the angle θ, the maximum height of H increases first and then decreases, that is, the convex function, which can be solved by the method of three points.

The Calc function here requires a cumbersome push-down formula:
s = l * cos (θ) + w * sin (θ)-X;
H = s * tan (θ) + w * cos (θ);
where S is the horizontal distance from the corner of the car's rightmost point, H is the highest distance from the bend, and θ ranges from 0 to 90.

POJ 3301 Texas Trip
http://acm.pku.edu.cn/JudgeOnline/problem?id=3301

Test instructions for a given n (n <= 30) point, the smallest square of the area with these points is calculated.

There are two solutions, one for approximation, that is, every m angle, the most consistent angle, then continue m, so the times, you can find a more accurate solution. (M probably take ten, Times take 30)

The second solution is the three-point method, the first rotation of the angle as long as 0-180 degrees, more than 180 degrees and the same as the previous. After the axis rotates, the coordinate transformation is:
X ' = x * cosa-y * SINA;
Y ' = y * cosa + x * SINA;

As to the question of whether the function is convex, why is convex, I can not give accurate proof, I hope that a passing Daniel pointing ~ ~

Update on examples (2010.5.5)
HDU 3400 Line Belt
http://acm.hdu.edu.cn/showproblem.php?pid=3400
The typical three-part method, first three points first line, find a point, and then according to this point repeatedly divided the second line can be, think of three points of thinking basic can be over.

For solving some practical problems, when the formula is difficult to deduce, the two-and three-part method can be used to solve some critical value accurately, and the efficiency is satisfactory.
(Transferred from Http://hi.baidu.com/czyuan_acm/blog/item/8cc45b1f30cefefde1fe0b7e.html)

Three-point Search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.