【ACWing】320. Energy Necklace

Subject address:

https://www.acwing.com/problem/content/322/

On the planet Mars, every Mars person wears an energy necklace with NN on the necklaceN energy beads. The energy bead is a bead with a head mark and a tail mark. These marks correspond to a positive integer. And, for two adjacent beads, the tail mark of the previous bead must be equal to the head mark of the next bead. Because only in this way, through the action of the sucker (a sucker is an organ for the Mars people to absorb energy), the two beads can aggregate into a bead, and at the same time release the energy that can be absorbed by the sucker. If the head of the previous energy bead is marked asmmm , the tail is marked asrrr , the head of the latter energy bead is marked asrrr , the tail is marked asnnn , the energy released after polymerization ism ∗ r ∗ nm*r*nmrn (Mars unit), the head of the newly generated bead is marked asmmm , the tail is marked asnnn . When needed, the Mars would use a suction cup to clamp two adjacent beads, and obtain energy through aggregation until only one bead remained on the necklace. Obviously, the total energy obtained by different polymerization sequences is different. Please design a polymerization sequence to maximize the total energy released by a string of necklaces. For example: LetN = 4 N=4N=4 4 4 The head and tail marks of the 4 beads are(2, 3), (3, 5), (5, 10), (10, 2) (2, 3), (3, 5), (5, 10). ), (10, 2)(23),(35),(510),( 1 0 , 2 ) . We use the mark⊕ ⊕ represents the aggregation operation of two beads,(j ⊕ k) (j⊕k)(jk ) meansjjj k k k The energy released after the two beads are polymerized. Article4 44 1 1 1 The energy released after the two beads are aggregated is:(4 ⊕ 1) = 10 ∗ 2 ∗ 3 = 60 (4⊕1)=10*2*3=60(41)=1023=6 0 . The total energy released by a polymerization sequence in which this string of necklaces can get the optimal value is((4 ⊕ 1) ⊕ 2) ⊕ 3) = 10 ∗ 2 ∗ 3 + 10 ∗ 3 ∗ 5 + 10 ∗ 5 ∗ 10 = 710 ((4⊕1)⊕2)⊕3)= 10*2*3+10*3*5+10*5*10=710((41)2)3)=1023+1035+10510=710

Input format:
The first line of input is a positive integer NNN represents the number of beads on the necklace. The second line isNNN positive integers separated by spaces, all numbers do not exceed1000 10001 0 0 0 ,iiThe number of i isiiThe head mark of i beads, wheni <N i <Ni<N o'clock,iiThe tail mark of i beads should be equal toi + 1 i+1i+Head mark of 1 bead,NNThe tail mark of N beads should be equal to1 1Mark the head of 1 bead. As for the order of the beads, you can determine it like this: put the necklace on the table without crosses, specify the first bead at will, and then determine the order of the other beads in a clockwise direction.

Output format: There
is only one line of output, which is a positive integer EEE is the total energy released by an optimal polymerization sequence.

Data range:
4 ≤ N ≤ 100 4≤N≤1004N100
1 ≤ E ≤ 2.1 ∗ 1 0 9 1≤E≤2.1∗10^9 1E2.1109

The idea is dynamic programming. The general technique for dealing with ring problems can be used, that is, copy a copy and receive it at the back. Although the input here is NNN number, but what we actually require is the lengthN + 1 N+1N+The maximum energy in the interval of 1 , so thisNNN numbers are repeated and spelled at the back to form a length of2 N 2NAn array of 2 N , which is transformed into a linear problem. Letf [i] [j] f[i][j]f [ i ] [ j ] is the merge interval[i, j] [i,j][i,The maximum energy that can be generated by j ] can be classified according to the median value of the last merge, and its range is[i + 1: j − 1] [i+1:j-1][i+1:j1],所以有: f [ i ] [ j ] = max ⁡ i + 1 ≤ k ≤ j − 1 { f [ i ] [ k ] + f [ k ] [ j ] + a [ i ] ∗ a [ k ] ∗ a [ j ] } f[i][j]=\max_{i+1\le k\le j-1}\{f[i][k]+f[k][j]+a[i]*a[k]*a[j]\} f[i][j]=i+1kj1max{ f[i][k]+f[k][j]+a[i]a[k]a [ j ] } Finally only need to returnmax ⁡ if [i] [i + N] \max_i f[i][i+N]maxif[i][i+N ] is fine. code show as below:

#include <iostream>
using namespace std;

const int N = 210;
int n;
int a[N];
int f[N][N];

int main() {
    
    
    cin >> n;
    for (int i = 1; i <= 2 * n; i++)
        if (i <= n) cin >> a[i];
        else a[i] = a[i - n];

    int res = 0;
    for (int l = 3; l <= n + 1; l++)
        for (int i = 1; i + l - 1 <= 2 * n; i++) {
    
    
            int j = i + l - 1;
            for (int k = i + 1; k <= j - 1; k++)
                f[i][j] = max(f[i][j], f[i][k] + f[k][j] + a[i] * a[k] * a[j]);
            
            if (l == n + 1) res = max(res, f[i][j]);
        }

    cout << res << endl;

    return 0;
}

Time complexity O (n 3) O(n^3)O ( n3 ), spaceO (n 2) O(n^2)O ( n2)

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/114583112