Talking about-two points to find a detailed explanation of the two points

Source: Internet
Author: User

One, what is the binary search.

1, we first introduce such a problem: if a certain subject score range: [0,100], now Xiaoming know his achievements, he let you guess his grades, if the guess high or low will tell you, with the fewest number of times to guess his grades, how you will set the plan. (excluding the luck ingredient and your understanding of Xiaoming's usual grades)

① The most stupid way of course is to guess from 0, always guessed 100 points, consider this to guess the least number of times: 1 (Luck quack good), 100 (Luck Gaga back);

② In fact, we do not know the other side of the level of the conditions, our every guess is to try to not need to guess the part to get rid of, but also to xiaoming do not understand, do not know its level in the end, then we consider dividing the score evenly,

The fraction interval is divided into 2, the first time we guess the score will be 50, when the answer is low, we will be the fraction of the region from "0,100" to "51,100"; when the answer is high, we determine the fractional region to "0,49". This reduces the extra 50 guesses (from 0 to 49) (or from 51 to 100) at a draught.

③ then let's assume that when we finish guessing 50 points, the answer is low. Then we need to continue to guess Xiaoming's score in the "51,100" points, the same, we continue to binary, the second time we will guess 75 points, when the answer is low, we will its fractional region from "51,100" to "76,100" When the answer is high, we determine the fraction area to "51,74". This reduces the excess conjecture (from 51 to 74) (or from 76 to 100) at a draught.

The ④ continues until the reply is correct, and this consideration is clearly optimal,


2, then we will have a maximum number of inquiries.

We might as well write a code to test:

#include <stdio.h>
int main ()
{for
    int y=0;y<=100;y++)//defines a fraction of
    {
        int l=0;
        int r=100;
        int mid;
        int cont=0;//Statistics guess how many times while
        (r>=l)
        {
            cont++;
            Mid= (L+R)/2;
            if (mid>y) r=mid-1;//If the current conjecture is large, the next step in the range "L,mid-1" is to determine the next value to be guessed
            else if (mid<y) L=mid+1;<span style= " Font-family:arial, Helvetica, Sans-serif; " >//if the current conjecture is small, then the next step in the range "Mid+1,r" to determine the next guess value </span>
            else break;//guessed, and then out of
        printf ("Y: %d cont:%d ", y,cont);
        if (y%4==0&&y!=0) printf ("\ n");
    }


The results are obvious:



Will operate up to 7 times, in fact, because every time we throw away half the interval of the currently determined interval as an impossible solution, it is equivalent to asking for the maximum number of operations, that is, in the interval, the maximum number of half can be thrown away, so that is 1001 straight divided by 2, until it can not be removed.

So this operation, in fact, is equivalent to the request of a log2 (100) ≈7.


3, then the time complexity of binary lookup: O (LOGN); n indicates interval length.


Two or two-point search of the classic application and the scope of application;


1, we again introduce such a problem: if you give you a sequence of N (1<=N<=1E5), there are Q queries (1<=Q<=1E5), each time the query input a value x (0<=X<=1E18), Let you find the first number larger than x to ensure that there is a solution.

Time limit of 1000ms


Sample input:

10

6 7 8 9 10 1 2 3 4 5

1

3


① if we do it with the simplest of ideas:

Each query corresponds to a for sweep sequence, recording all the numbers larger than X, maintaining a minimum value from it.

Its time complexity is O (QN), the worst case operation times are 10^10, however, the general oj1000ms can withstand the number of operations is about 10^8, it is obvious that this will time out.

So if we can reduce complexity in all groups and think of a binary lookup, then the time complexity will turn to: O (QLONGN), the worst-case operation is about 10^6, and now it's obviously not going to happen again.


Then consider the binary lookup:

③ if we were to guess the first number of X's by the way we guessed the score above, we must first determine whether the sequence is incremented or decremented. Because suppose we now do the two points of the above process in the current sequence:

The interval length is obviously 10, so let's look at the middle number (fifth): The relationship between 10 and 3, obviously 10>3, but the interval range we're going to determine next. Is "0,4" good, or "6,10" good. Although our eyes can be seen, but the problem to the code and computer, they do not know what to do.

But if we can guarantee that the sequence is incremented, then this time the sequence is:

1 2 3 4 5 6 7 8 9 10

The interval length is obviously 10, so let's look at the middle number (fifth): The relationship between 5 and 3, obviously 5>3, then how do we determine the next range of the interval to find. Since the first second fourth number is less than the fifth number, then obviously the value I'm looking for should be in "0,4", or 5 itself, then we'll write the value 5 as a workable answer. Rather than "6,10"; this time we determine the interval "0,4" after, and so on, and so on, and so on, if a "mid" >x, then we will be the number of the answer, then the last recorded value is the answer.


Code:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[1000400];
int main ()
{
    int n;
    while (~SCANF ("%d", &n))
    {for
        (int i=0;i<n;i++)
        {
            scanf ("%d", &a[i]);
        Sort (a,a+n);
        int q;
        scanf ("%d", &q);
        while (q--)
        {
            int x;
            scanf ("%d", &x);
            int l=0;
            int r=n-1;
            int mid;
            int ans;
            while (r-l>=0)
            {
                mid= (l+r)/2;
                if (a[mid]>x)
                {
                    r=mid-1;
                    Ans=a[mid];
                }
                else l=mid+1;
            }
            printf ("%d\n", ans);
        }
    }



④ summarizes the above process:

First, the sequence is changed to a monotone sequence, then the binary search, the feasible solution is recorded, the last record of the feasible solution is the answer.


⑤ so want to do a binary lookup of a sequence, then we must first satisfy the monotony of this sequence. (use of two-point lookup)


I hope that the article to understand that the two-point search will have an auxiliary, here and then list two need to find a two-point search you can correspond to the iron strike a wave:

Harbin Institute of Science and Technology OJ 1187 (don't forget to discuss monotonicity first)

HDU 2141


2016/10/7 20:51

mengxiang000000



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.