Bipartite pose = _ =

Source: Internet
Author: User

Bipartite pose = _ =

Binary Search:


1: The first number greater than or equal to xx

int bin_s(int xx){        int l=1,r=10;        int ans=-1;        while(l<=r){                int m=(l+r)>>1;                if(a[m]>=xx) ans=m,r=m-1;  //!!!                else l=m+1;        }        return ans;}e.g:  int a[]={0,1,2,5,5,5,5,7,8,9,10};in:           5out:        3
2: The first number greater than xx
Int bin_s (int xx) {int l = 1, r = 10; int ans =-1; while (l <= r) {int m = (l + r)> 1; if (a [m]> xx) ans = m, r = m-1 ;//!!! Else l = m + 1;} return ans;} e. g: int a [] = {, 5, 5, 10 };
in:           5out:        7

3: The first number smaller than or equal to xx (close to xx)
Int bin_s (int xx) {int l = 1, r = 10; int ans =-1; while (l <= r) {int m = (l + r)> 1; if (a [m]> xx) r = m-1; else ans = m, l = m + 1;} return ans;} e. g: int a [] = {, 10 };
in:           5out:        6
4: the first number smaller than xx (close to xx)
int bin_s(int xx){        int l=1,r=10;        int ans=-1;        while(l<=r){                int m=(l+r)>>1;                if(a[m]>=xx) r=m-1;                else ans=m,l=m+1;        }        return ans;}e.g:  int a[]={0,1,2,5,5,5,5,7,8,9,10};
in:           5out:        2



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.