Description of the algorithm two or three things about two points

Source: Internet
Author: User

  About two points

   Two points, is a rogue algorithm, this method is suitable for direct calculation (guessing, Monte, test) the answer, and then directly obtained the optimal solution,

This method has the ability to easily prove that the answer is feasible (O (n), O (1)).

(In the NOIP2015 exam when the problem, put down a push Konjac konjac (including me))

Conditions of use of two points

1. As mentioned above, the question of whether the answer is easy to prove feasible, to solve together, for the sub-problem has this nature, can also be solved in this way, miraculous. (relevant content below)

2. This is also an extremely feasible approach to the problem of increasing or decreasing the value of the query interval .

A few things about writing and STL

Every God Ben almost have their own personality of the dichotomy, because this thing can be different places too much, but in itself, but also is the same truth.

1. About the <algorithm>, the STL's wording

In the library function of C + +, there are two dichotomy functions, called Upper_bound and Lower_bound, respectively.

  

Upper_bound

This library function is defined as: in the current non-descending sequence of two addresses, return the first is greater than the current value of the address .

Use format: Upper_bound (A+1,a+n+1,num);

Where a array is the non-descending array we want to query, and 1, n+1 is the interval address we are looking for (also the start / End position of the array variable name + query), num is the value we want to query.

The specific library function code is as follows

1 intUpper_bound (int*array,intSizeintkey)2 {3     intFirst =0, Len = size-1;4     inthalf, middle;5 6      while(Len >0){7half = Len >>1;8Middle = first +half;9         if(Array[middle] > key)//The median is greater than key and is found in the left half of the sequence that contains the last. TenLen =half; One         Else{ AFirst = middle +1;//The median is less than or equal to key and is found in the right half of the sequence.  -Len = len-half-1; -         } the     } -     returnFirst ; -}

Lower_bound

This library function is defined as: in the current non-descending sequence of two addresses, return the first greater than or equal to the current value of the address .

Use format: Lower_bound (A+1,a+n+1,num);

The definition of the meaning of each array is basically consistent with the above

The library function code is as follows

1 intLower_bound (int*array,intSizeintkey)2 {3     intFirst =0, middle;4     inthalf, Len;5Len =size;6 7      while(Len >0) {8half = Len >>1;9Middle = first +half;Ten         if(Array[middle] <key) {      OneFirst = middle +1;  ALen = len-half-1;//Find in the right sub-sequence -         } -         Else theLen = half;//Find in the left sub-sequence (contains middle) -     } -     returnFirst ; -}

  2. About Personal code habits

Here are two kinds of preyw and Szl two God Ben different dichotomy, here for reference

1 intPrey_bs (intLintRintkey)2 {3      while(L < R-1 )4     {5m = (l+r) >>1;6         if(A[m] <key)7L = m +1;8         Else9R =m;Ten     } One     returnl; A}
1 intZl_bs (intLintRintkey)2 {3     intans;4      while(L < =R)5     {6m = (L + r) >>1;7         if(A[m] <key)8L = middle +1;9         Else{Tenr = M-1; OneAns =R; A         } -     } -     returnans; the}

These two methods may be different from the one written by God Ben, but that's probably the case.

In general, the dichotomy is still a tool for solving problems, if the wording of the crossing is not clear enough, you can use the above STL and God Ben to replace their original, may have a better effect

Then the example is all Openjudge and Codevs's two-part question.

I wish you all a happy brush.

Description of the algorithm two or three things about two points

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.