Two-point Search summary

Source: Internet
Author: User

First look at the general wording of the binary search

#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < Algorithm> #include <vector>using namespace std; #define N 100005#define ll __int64int n,a[n];int findd (int x)  {    int l=0,r=n-1;    while (l<=r)    {        int mid= (l+r) >>1;        if (a[mid]==x)            return mid;        if (a[mid]>x)            r=mid-1;        else            l=mid+1;    }    return-1;   }int Main () {    int i,m,x,t not found;    while (~SCANF ("%d", &n))    {for        (i=0;i<n;i++)        {            scanf ("%d", &a[i]);        }        Sort (a,a+n);        scanf ("%d", &m);        while (m--)        {            scanf ("%d", &x);            T=FINDD (x);            printf ("%d\n", t);        }    }    return 0;}

A generic binary lookup can only find the target value, but the subscript returned when the target value repeats appears is indeterminate, and the following notation returns the subscript for the first occurrence of the target value. When the target value does not appear, returns the subscript of the element that is larger than the target value or returns the boundary value N;


#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < Algorithm> #include <vector>using namespace std; #define N 100005#define ll __int64int n,a[n];int findd (int x) { C0/>int l=0,r=n-1;    while (l<=r)    {        int mid= (l+r) >>1;        if (a[mid]>=x)            r=mid-1;        else            l=mid+1;        /*if (a[mid]<x)       //This writing also above the same effect            l=mid+1;        else            r=mid-1;*/    }    return l;   Returns the subscript for the target value, and returns the first occurrence of the subscript if more than one value is the same. }int Main () {    int i,m,x,t;    while (~SCANF ("%d", &n))    {for        (i=0;i<n;i++)        {            scanf ("%d", &a[i]);        }        Sort (a,a+n);        scanf ("%d", &m);        while (m--)        {            scanf ("%d", &x);            T=FINDD (x);            printf ("%d\n", t);        }    }    return 0;}


Two-point Search summary

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.