Binary-thirds Find

Although the title says is binary-thirds find it much because it is water-thirds of the currently used

Let's look at two points

Bipartite

Dichotomy refers to the interval [a, b] a continuous and f (a) · f (b ) <0 a function of y = f (x), by the continuous function f (x) where a zero interval is divided into two, the two end sections gradually approaching zero, and thus obtain zero approximation method.

Conditions may be understood to correspond to the query interval [l, r] should meet the f (l) * f (r) <0, mid = (l + r) >> 1

f (mid) <0 interval update query [l, mid]; 

f (mid)> 0 is updated query interval [mid, r]; 

f (mid) = 0 then the found zero

Generally when we see the subject politely ask you to seek the maximum value minimum or minimum maximum time you can use half the

 

Classroom example (1) luogu 1024 Cubic Equations

The answer is in the range [-100,100], the absolute value of the difference between the two roots> = 1, so the size of each interval is at most 1, in a solution.

This is the title of a 0011 model, we use dichotomy keep close to zero, the determined minimum range is the emergence of the first l 1 that is what we want to answer.

#include <bits / STDC ++ H.>
 the using  namespace STD;
 #define MAXN 500 010
 const  Double EPS = 1E- . 4 ; // corresponds to define a precision constants from 0.0001 

Double A, B, C, D, L, R & lt, MID; 

Double F ( Double X) { // original equation 
    return X * X * X * X * X * A + B * X + C + D; 
} 

int main () { 
    CIN >> >> B >> A >> C D;
     for ( int I = - 100 ; I <= 100 ; I ++) { // enumeration root section where 
        L = I; // left l-mid section
        = L + R & lt . 1 ; // Right-R & lt interval MID 
        IF (F (L) == 0 ) { // find zero 
            the printf ( " % .2f " , L);
             Continue ; 
        } 
        IF (F (L) F * ( R & lt) < 0 ) { // satisfies the condition half 
            the while (rl is an> EPS) { // card accuracy with 
                MID = (L + R & lt) / 2 ;
                 IF (F (MID) * F (R & lt) <= 0 ) L mid =; // if mid-r satisfies bipartite conditions described in this range answer, the update interval to the left mid to research 
                the else R & lt mid =; // if l-mid condition is satisfied, the interval will be updated to the right mid
            } 
            The printf ( " % .2f " , L); // this model 0011 appears first is also desired 1 
        } 
    } 
    return  0 ; 
}

 

Class example (2) luogu 1182 series segments Section II

Subject requirements segment, seeking the maximum and minimum of each segment

 

Sample explained:

About maximum minimum:

For example a number of columns 42451 to be divided into 3 sections

Segment which is as follows:

[4 2][4 5][1][42][45][1]

And for the first segment 6 , the first two segments and is 9 , the first segment is 3 and 1, and a maximum of 9 .

Segment which is as follows:

[4][2 4][5 1]

For the first stage and 4, two segments and is 6 , the 3 segments and of 6 and a maximum of 6 .

And in any case the segment, the maximum value of not less than 6.

To get the number of columns can be 42,451 to be divided into three segments, the minimum and maximum value of 6.

 

We can put each piece as a vessel, as a container capacity of each segment and each put a number capacity will be less, when the capacity is not sufficient to put down this number, we will take a new container

There is a standard container capacity, when a segment becomes the minimum standard, we found the answer

code show as below:

#include <bits / STDC ++ H.>
 the using  namespace STD;
 #define MAXN 500 010 int now, MID, L, R & lt, n-, m, SUM, V; 
 int A [MAXN]; BOOL Jug () { // determine if the operation legitimate 
    now = MID, sum = . 1 ; // we now define as a capacity, sum for counting for ( int I = . 1 ; I <= n-; ++ I) {
         IF (now <a [I]) { / / when the storage capacity is not enough when the next digital 
            SUM ++; // can be divided for some 
            now-MID = a [I]; // update size limit         }
         the else NOW- = a [I]; //




    
Updates the remaining capacity 
    } 
     return sum <= m; // returns true when the sum magnitude value of legitimate 
} 

int main () { 
    CIN >> >> n- m;
     for ( int I = . 1 ; I <= n-; ++ I ) { 
        CIN >> A [I]; 
        L = max (L, A [I]); // update interval left 
        R & lt = A + [I]; // advance the right section rearwardly 
    }
     the while (L <R & lt) { / / ensure dichotomy exists 
        MID = (L + R & lt) >> . 1 ;
         IF (Jud ()) = MID R & lt; // if the return value is true updating the right section 
        the else L = MID + . 1; // contrary update interval left 
    } 
    COUT << L << endl;
     return  0 ; 
}

The first pit. . Late continuously updated

 

Guess you like

Origin www.cnblogs.com/Starlight233/p/11284742.html