Three-way search

Source: Internet
Author: User

As the most common method of divide and conquer, the bipartite method is applicable to monotonic functions and is used to approximate the value of a certain point. However, when a function is a convex function, the bipartite method cannot be applied. In this case, the three-way method can be used to show its strength "~~

, Similar to the binary definition of left and right, mid = (left + right)/2, midmid = (Mid + right)/2; If mid is near the extreme point, Right = midmid; otherwise (that is, the midmid is close to the extreme point), left = mid;

ProgramThe template is as follows:

Double calc (type)
{
/* Calculate based on the meaning of the question */
}

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 that the maximum extreme value is obtained.
If (mid_area> = midmid_area) Right = midmid;
Else left = mid;
}
}

The specific implementation of the three-way method is analyzed based on several OJ questions.

BUAA 1033 easy problem
Http://acm.buaa.edu.cn/oj/problem_show.php? C = 0 and P = 1033

Finding a point on a line segment is the least distance from a given point P. Obviously, the convex function is solved by three points.
The calc function is used to calculate the distance between a certain point and a certain point.

Zoj 3203 light bulb
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3203.

The longest length of the Shadow L.
According to the figure, it is easy to find that when the light, the person's head and the corner are in a straight line (assuming that the person is standing at a point), the length at this time is the maximum length of the shadow on the ground. When a person goes to the right, the shadow begins to be projected onto the wall. When a person sticks to the wall, the length of the shadow is the height of the person. So when a person moves from point A to the wall, the function increments first and then decreases, which is a convex function, so we can use the three-way method to solve the problem.

Only the calc function is provided below, and Other templates can be directly set.
Double calc (Double X)
{
Return (H * D-H * X)/(d-x) + X;
}

Heru 5081 turn the corner 08 Harbin Regional Network Competition
Http://acm.hrbeu.edu.cn/index.php? Act = problem & amp; id = 1280

The turning of a car is determined by X, Y, L, and D. First, when X or Y is less than D, it must not.
Secondly, we find that with the increase of angle θ, the maximum height H increases first and then decreases, that is, the convex function. We can use the three-way method to solve the problem.

The calc function here requires a complicated formula:
S = L * Cos (θ) + W * sin (θ)-X;
H = S * Tan (θ) + W * Cos (θ );
S is the horizontal distance from the rightmost point of the car to the corner, H is the highest distance from the inner inflection point, and θ ranges from 0 to 90.

Poj 3301 Texas trip
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3301

Given n (n <= 30) points, find the square with the smallest area filled with these points.

There are two solutions, one is the approximation method, that is, the M-point angle each time, find the most consistent angle, and then continue M-points, so that the times, you can find a more accurate solution. (M is about 10, and times is about 30)

The second solution is the three-way method. First, the rotation angle must be between 0 and 180 degrees, and the rotation angle must be greater than 180 degrees. After the coordinate axis is rotated, the coordinates are changed:
X' = x * cosa-y * Sina;
Y' = y * cosa + x * Sina;

As to whether the function is convex or not, why is it convex? I cannot provide an accurate proof. I hope someone passing by can give me some advice ~~

Example Update (2010.5.5)
HDU 3400 line belt
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3400
The typical three-way method is to first take the first line segment by three minutes, find a point, and then use the second line segment by the third. The idea of the three points is basically enough.

For solving some practical problems, when the formula is difficult to derive, the two-or three-way method can accurately solve some critical values, and the efficiency is satisfactory.
(From http://hi.baidu.com/czyuan_acm/blog/item/8cc45b1f30cefefde1fe0b7e.html)

Related Article

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.