"Algorithm Summary" two-point search continuation

Source: Internet
Author: User

All we talked about was looking for a specific value, mostly talking about minimizing the maximum and maximizing the minimum value problem. The most error-prone problem is the termination condition, the interval form, the return lb or UB, or the need to add minus one. The template is listed here

One: two points of integral type

1. Minimizing the maximum value problem

Form one: (ub-lb) > 1 interval for (LB, UB] results for UB

while (ub-lb > 1) {    int mid = lb + (ub-lb)/2;    if (C (mid))         UB = mid;    else        lb = mid;}
Terminate loop when ub = = lb + 1

Form two: (ub-lb) > 0 interval for [lb, UB] results for UB

while (ub-lb > 0) {    int mid = lb + (ub-lb)/2;    if (C (mid))         UB = mid;    else        lb = mid + 1;}
Terminate loop when ub = = LB

  

2. Maximizing the minimum value problem

Form one: (ub-lb) > 1 interval for [lb, UB) results for LB

while (ub-lb > 1) {    int mid = lb + (ub-lb)/2;    if (C (mid))         UB = mid;    else        lb = mid;}
Terminate loop when ub = = lb + 1

Form two: (ub-lb) > 0 interval for [lb, UB] results for LB

while (ub-lb > 0) {    int mid = lb + (ub-lb + 1)/2;    if (C (mid))         lb = mid;    else        UB = mid-1;} Terminate loop when ub = = LB

Minimize the maximum value by maintaining the location of UB, maximizing the minimum value by maintaining the position of lb.

The core of the dead loop is the way mid is valued: Mid = lb + (ub-lb)/2, if ub = = lb + 1 at a time, then because the mid is rounded down, it causes mid = lb; If the If statement causes lb = mid to occur, then it enters the dead loop, mid = lb, and then lb = Mid. The same mid = lb + (ub-lb + 1)/2; if there is UB = mid operation, it is possible to enter a dead loop. However, the termination condition of the ub-lb > 1 will cause this situation to no longer occur, because when ub = = lb + 1 o'clock, there is no operation of mid = lb or mid = UB due to a loop exit that does not meet the cyclic conditions.

When the interval is closed, preventing entry into the dead loop means that mid = = lb && mid = = UB is not established

1) Minimize Max mid-down rounding (Maintenance UB)

If the IF condition is true, UB needs to be maintained so that UB = mid; If this is not true, LB = mid + 1; Since the mid-rounding is only possible to take lb, lb = mid + 1 must be made to avoid a dead loop, and since the mid does not meet the loop condition, it is not necessary to consider this value because we want to minimize the value that satisfies the condition.

2) Maximize minimum mid up rounding (maintain lb)

If the IF condition is true, B must be maintained so that lb = mid; If this is not true, UB = mid-1; Since mid-rounding is only possible for UB, UB = mid-1 must be made to avoid a dead loop, and since the mid does not meet the loop condition, it is not necessary to consider this value because we want to maximize the value that satisfies the condition.

    

Two: two points of floating point number

Floating-point numbers are usually not so-called boundary problems.

Form one:

for (int i = 0; i < i++) {    Double mid = lb + (ub-lb)/2;    if (C (mid))         UB = m;//lb = m;  According to problem    else        lb = m;//ub = m;  According to Problem}

100 cycle accuracy of up to 10e-30

Form two:

while (ub-lb > EPS) {    double mid = lb + (ub-lb)/2;    if (C (mid))         UB = m;//lb = m;  According to problem    else        lb = m;//lb = m;  According to Problem}

EPS can be set according to the actual situation, it is important to note that EPS if too small, because the floating point machine implementation of the cause, may lead to a dead loop.

"Algorithm Summary" two-point search continuation

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.