[2] A summary of the binary classification

Source: Internet
Author: User

Here are some of my experiences with the binary classification. It seems that everyone writes two points in comparison, and the upper and lower circles make a mistake ~ TLE or something.

First, write the following two integers:

[The following programs search for x in the range [l, r]. By default, the data sequence is not decreasing]

(1) returns the subscript (exists and unique) of a number in a binary search interval.-1 is not returned:

int search(int l,int r,int x){int mid;while (l<=r){mid=(l+r)>>1;if(a[mid]==x) return mid;if(a[mid]<x) l=mid+1;else r=mid-1;}return -1;}

This should be the best binary write. Note that the condition l <= r is not l <r. I don't know much about it. I can understand it at a glance.

In fact, this program can be changed to the earliest subscript and the latest subscript to query a number.

(2) The maximum value of <= x in the query interval (if there are multiple maximum values, the rightmost coordinate is returned ):

int search(int l,int r,int x){int mid;while (l<r){mid=(l+r+1)>>1;if(a[mid]<=x) l=mid;else r=mid-1;}return l;}

Note the following two points:

(1) If you want to query on [l, r], the recommended parameter to be passed in is the l-1.r, so if the return value is l-1, it indicates that there is no <= x value, alternatively, you can choose to judge before the second to determine whether a solution exists before the second.

② Mid = (l + r + 1)> 1 instead of mid = (l + r)> 1. If the last method is used, assume that the interval is reduced to 2 and 3, and then the mid is 2. If a [mid] <= x is true, then l = mid, it will become an endless loop, so we need to + 1 and then divide 2.

(3) the minimum value> = x in the query interval (returns the leftmost coordinate if there are multiple minimum values ):

int search(int l,int r,int x){int mid;while (l<r){mid=(l+r)>>1;if(a[mid]>=x) r=mid;else l=mid+1;}return l;}

The program is basically the same as (2), but the condition is changed to r = mid and l = mid + 1, so mid should be changed to mid = (l + r)> 1, otherwise, an infinite loop occurs. If the query interval is [l, r], the parameter l, r + 1 is passed. If the return value is r + 1, no solution is available.

In this way, we only need to control the upper and lower bounds and take the intermediate conditions, so we can drop the second point by 1Y.

The second part of the real number is relatively easy to write. It is good to control the boundary according to the precision required by the question. Generally, there will be no endless loops.

While (fabs (r-l)> EPS) // EPS is the precision required by the question {mid = (l + r)/2; if (check (mid )) l = mid; else r = mid ;}

Some question accuracy cards may be more fun and can be controlled cyclically. In fact, even in the real number field, the convergence of binary is very fast, it can be reduced to the solution after hundreds of times of binary execution.

for(int i=1;i<=200;i++){mid=(l+r)/2;if(check(mid)) l=mid;else r=mid;}

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.