[Offer] to prove safety cut the rope (to tell the truth, or do not understand)

Title: Here n is a length of rope, the rope please cut segment m (m and n are integers, n> 1 and m> 1) the length of each rope is referred to as k [0], k [1 ], ..., k [m]. Will k [0] * k [1] * ... * k [m] maximum possible product is how much? For example, when the length of the rope 8, we cut it into lengths of three sections 2,3,3 of the product obtained at this time is the maximum 18.

 

Dynamic Programming

  Always from the smallest to solve the problems began, and the storage sub-optimal solution to the problem has been resolved down (one-dimensional or two-dimensional array), and the sub-optimal solution to the problem of combining gradually solve the big problem.

  Dynamic Programming features:

  1. Target problem is a problem of seeking the optimal solution: target problem is to find a product of the segments cut rope maximum length
  2. The optimal solution is the optimal solution to the overall problem of the respective sub-problem since: i in the first position of the scissors, and the rope is divided into i ni; optimization of the method for the same i and ni, respectively, cut into segments ... ...So on and so forth
  3. The problem is divided into a large number of small problems, there is a small problem between the overlapping smaller sub-problems: Suppose the length of the rope 10 first, into the first knife 4 and 6; the second blade 4 minutes to give two 2 ; third blade 6 minutes, 2 and 4 have then ...... f (2) is a problem common sub F (4) F and (6) of
  4. From top to bottom analysis of the problem, to solve the problem from the bottom up: Avoid repeating subproblems

 

Greedy algorithm

  Each step can make a greedy choice, based on this selection, you can get the optimal solution.

  For example the length of the rope 10, if the length of rope is greater than 3, the length of the cut section 3, if the residual is still greater than the length of the rope 3, the cutting section 3 ...... continued until the remaining length of the rope is less than 3  

 

// Dynamic Programming

  f(n) = max(f(i) * f(n-i)) , 0<i<n

    In order to avoid the problem repeats, to obtain f (2), f (3) then get f (4), f (5) ......

  When the length of the rope 2 when only two scissors 1 ===> f (2) = 1;

  When the time length of the rope 3, section 2, and can be cut or a section of three segments 1 ===> 1 * 2> 1 * 1 * 1 ===> f (3) = 2

{Solution class 
public: 
    int maxProductAfterCutting_solution (int length) { 
        IF (length <2) 
        { 
            return 0; 
        } 
        IF (length == 2) 
        { 
            return. 1; 
        } 
        IF (length ==. 3) 
        { 
            return 2; 
        } 
        
        // sub the optimal solution storage problems in the product array 
        int * = product new new int [length +. 1]; 
        product [0] = 0; 
        product [. 1] =. 1; 
        product [2] = 2; 
        product [. 3] =. 3; 
        // product [i] denotes the i-th element of the product of the maximum segment length to the length of the rope is cut into segments of i, F (i) 
        int max = 0; 
        // i prior to evaluation for each j, f (j) are It has been solved out, and the results stored in the product [j] in
        for (int. 4 = I; I <= length; I ++) 
        { 
            // Determine all possible f (j) * f (ij ) and compare the maximum value 
            max = 0; 
            for (int. 1 = J; J = I / 2; J ++) 
            { 
                int tmp = Product [J] * Product [I - J]; 
                IF (max <tmp) 
                { 
                    MSX = tmp; 
                } 
                Product [I] = max; 
            } 
        } 
        max = Product [length]; 
        Delete [] Product; 
        
        return max; 
        
    } 
};

  

 

// greedy algorithm

  When n> = 5, as much as the length of the rope 3 is cut, when the remaining length of the rope 4, the two lengths of rope cut the rope 2

{Solution class 
public: 
    int maxProductAfterCutting_solution (int length) { 
        IF (length <2) 
        { 
            return 0; 
        } 
        IF (length == 2) 
        { 
            return. 1; 
        } 
        IF (length ==. 3) 
        { 
            return 2; 
        } 
        
        // make may take many cut length of the rope section 3 
        int length = timesOf3 / 3; 
        
        // when the length of the rope when the last remaining 4: cut the rope 2 of length two, since 2 * 2> 1 * . 3 
        IF ((length - timesOf3 *. 3) ==. 1) 
        { 
            timesOf3 - =. 1; 
        } 
        int timesOf2 = (length - timesOf3 *. 3) / 2;
        
        // returns x to the power y. 
        return ((int) (POW (. 3, timesOf3)) * (int) (POW (2, timesOf2))); 
    } 
};

  

 

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11415465.html