Experience interval dp

A. Energy necklace

https://www.luogu.org/problem/P1063 

(This question and purple book the best chain matrix multiply like)

① Sample analysis:

4
2 3 5 10 

We put it into four multiplication expression:

2*3   3*5  5*10 10*2

It requires 3 multiplications merger:

10 * 2 * 33 * 55 * 10 +60 energy

3 * 2 * 10 * 55 * 10 * 10 + 3 * Energy 5

10 * 2 * 3 * 5 * 10 + 10 * 5 * Energy 10

The total energy is 710

In fact, the expression also can write

2 * 3 * 5 * 10 * 2 * 3 * 5 * 10, then we have to do is to add parentheses (back to optimal chain matrix multiplication),

Every calculated values ​​are added together,

   2*10*3

 +10*3*5

 +10*5*10

We set f [i] [j] to take the maximum energy from i to j obtained

边界:f[ i ][ i ]=0,f[ i ][ i+2 ]=data[ i ]*data[ i+1]*data[ i+2 ]; 

f[ i ][ j ]=max(f[ i ][ k ]+f[ k ][ j ]+data[ i ]*data[ k ]*data[ j ]);

  State transition:

for ( int L = . 3 ; L <= n-; L ++ )
        for ( int I = . 1 , J = I + L; J <= n-* 2 ; I ++, J ++) // do the termination condition labeled i <= n , and so there will be some inter-cell does not sweep out 
         for ( int K = I + . 1 ; K <J; K ++ ) 
         { 
             F [I] [J] = max ((F [I] [K] + F [K] [ J]) + D [I] * D [K] * D [J], F [I] [J]); 
         }

Finally, f [1] [1 + n] to f [n] [n + n] where to find out ans

(As for why is + n, the landlord think it is because you want to be n-1 times computation, if you have a better explanation, please leave a message

Is not that there is a problem, if the data is such that it

2 3

Do not worry: said Title: The first line is a positive integer N (4≤N≤100) N ( . 4 N . 1 0 0 ), indicates the number of beads on the necklace.

So you can do ac.

II. To be updated, the main floor to do other title

 

Guess you like

Origin www.cnblogs.com/Neptune0/p/11845093.html