"Two-point search learning"

Source: Internet
Author: User

Knowledge Preparation

With the introduction of the algorithm and the programming Zhu Ji Nanxiong, the concept and nature of cyclic invariant are described below.

Cyclic invariant is mainly used to help understand the correctness of the algorithm. Form very similar to mathematical induction, it is a need to ensure the correct assertion. For cyclic invariant, it is necessary to prove its three properties:

Initialize: It should be correct before the first iteration of the loop begins.

Hold: If it is correct before an iteration of the loop begins, it should also remain correct before the next iteration begins.

Termination: The loop can be terminated and the desired result can be obtained.

Scope of application

To find a specific value from an ordered array

Assume a solution and determine whether it is feasible

Maximize Minimum value

Maximize average

Examples

A monotone non-descending sequence with a given length of n a0,a1 ... An-1 and a number k, which satisfies the minimum I of the ai>=k condition. Does not exist, output n.

Restriction conditions

1<=n<=10^6

0<=a0<=a1....<=an-1<10^9

0<=k<=10^9

First see the data range of the topic, and then according to the needs of the topic-Find, you should think of binary search.

It is easy to time out if you simply use sequential lookups.

We assume that the input n=5 a=2,3,3,5,6 k=3

We first look at the value of N/2, if a[n/2]>=k, we can know the solution is not greater than N/2.

Conversely, if a[n/2]<k, we can know that the solution is greater than N/2.

With such a simple comparison, the search scope of the data is reduced by half.

After that, the final solution is finally obtained from the O (log n) comparison.

Code implementation I will carry the petition above =. =

voidsolve () {//scope of initial dissolution    intLB =-1, UB =N; //repeat the loop until the solution exists in a range of no more than 1     while(Ub-lb >1){        intMid = (Lb+ub)/2; if(A[mid] >=k) {            //If the mid satisfies the condition, the scope of the solution becomes (Lb,mid] left open right closedUB =mid; }Else{            //if the condition is not met, the range of the solution becomes (Mid,ub]LB =mid; }} printf ("%d\n", UB);}
finding a value from an ordered array

Let's take a look at one of the most common problems you'll encounter when writing a question ———— maximize minimum (minimize maximum value)

Example Source: POJ 2456

I wrote the question: The farmer John built a hut with N barns. The barn is lined up on a line and the No. I Barn is in Xi's position. But his M-cows were not satisfied with the huts, so they often attacked each other. In order to prevent the injury between the cows, John decided to put each cow in the barn as far away from the other cows as possible. That is to maximize the distance between the two cows directly. (2<=2<=100000 2<=m<=n 0<=xi<=10^9)

Not to be continued

"Two-point search learning"

Related Article

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.