Luo Gu P1063 energy necklace (interval dp)

Subject description:

Given a string of sequence X [], where each Xi considered considered a bead, each bead contains two parameters, head and tail, the tail before a value is a value of the head, presenting an annular bead (a necklace), so in the end of the tail is a first bead head. when the tail corresponding head encounters will be released

Xi.head * Xi.tail * X ++ i.head, after which the two adjacent beads will become a new Xp, its argument is Xp.head = Xi.head, Xp.tail = X + + i.tail, asked the whole necklace merge into the biggest energy at only one time capable of producing.

Topic link: P1063

As for the order of the beads, so you can determine: the necklace on the table, do not appear cross, beads optionally specified first, then determine the order in a clockwise direction other beads.

Output Format

Positive integer a E (E≤2.1 × (10). 9) E (E≤2.1 \ Times (10) ^. 9) E ( E 2 . . 1 × ( . 1 0 ) . 9 ), an optimal order of polymerization of the released of the total energy.

in:

4
2 3 5 10

out:

710

Thinking: DP range, a large inter-cell interval is calculated as the optimal substructure, the entire solution is equivalent to looking for the subject 1 ~ E from which the beginning to merge, we dp [i] [j] represents a combined optimum section i to j, j to select k i begins incorporated state transition equation: dp [i] [j] = max (dp [i] [j], dp [i] [k] + dp [k + 1] [j] + a [i] .h * a [j] .t * a [i + k] .t)

Then the algorithm generally decided on O (n ^ 3) ideas

#include <bits/stdc++.h>
using namespace std;
struct node//存珠子
{
    int h;
    int t;
} e[1005];
int n;
int dp[205][205];
int total;
int main()
{
    cin>>n;
    for(int i=1; i<n; i++)
    {
        int x;
        cin>>x;
        if(i==1) 
        { 
            Int Y; 
            CIN >> Y; 
            E [I] .h = X; 
            E [I] .T = Y;
             Continue ; 
        } 
        E [I] .h = E [I- . 1 ] .T; 
        E [I ] .T = X;
         IF (I == N- . 1 ) 
        { 
            E [n-] .h = E [N- . 1 ] .T; 
            E [n-] .T = E [ . 1 ] .h; 
        } 
    } // enter the first and last made a special sentence 
    for ( int i =. 1 ; I <n-; I ++ ) 
        E [I + n-] = E [I]; // This step is very critical, the equivalent split ring, the whole sequence of the tail portion connected to the first to reach the annular 
    for ( int I = . 1 ; I <= n-; I ++ )
         for ( int J = . 1 ; J < 2 * Ni; J ++ )
             for ( int K = J; K <I + J; K ++ ) 
                DP [J] [J + I] = max (DP [J] [J + I], DP [J] [K] + DP [K + . 1 ] [J + I] + E [J] .h * E [K] .T * E [I + J] .T);
     for ( int I = . 1 ; I <= n-; I ++ ) 
              Total=max(total,dp[i][i+n-1]);
    cout<<total;

    return 0;
}

 

 

 

Guess you like

Origin www.cnblogs.com/iloveysm/p/12319245.html