1073. Energy Necklace Description
On Mars planet, each Mars person carries a string of energy necklaces. There are n energy beads on the necklace. The energy bead is a bead with a head mark and a tail marker, which corresponds to a positive integer. Also, for two adjacent beads, the tail mark of the previous bead must be equal to the head mark of the latter bead. Because this is the only way that the two beads can be aggregated into a bead and release the energy that can be absorbed by the suction cup, by means of the suction cup, which is an organ of the Mars Human Energy absorption. If the head of the previous energy bead is marked M, the tail mark is R, the head of the latter energy bead is labeled R, and the tail mark is n, then the energy released by the aggregation is MXRXN (Mars Unit), the newly generated bead has a head labeled M, and the tail mark is n.
When needed, the Mars man uses a suction cup to clamp the two adjacent beads, and the energy is obtained by polymerization until only one bead is left on the necklace. Obviously, the total energy from different aggregation sequences is different, please design an aggregation order so that the total energy released by a string of necklaces is the largest.
For example: The head mark and tail mark of the n=4,4 beads are (2,3) (3,5) (5,10) (10,2).
We use a tick to indicate the aggregation of two beads, (j⊕k) represents the energy released by the aggregation of the j,k two beads. Then the energy released by the 4th and 12 beads after aggregation is: (4⊕1) =10x2x3=60.
This string necklace can be obtained by an aggregation order of the optimal values for the total energy released ((4⊕1) ⊕2) ⊕3) =10x2x3+10x3x5+10x5x10=710
Input Format
The first line of the input file is a positive integer n (4≤n ≤ ), indicating the number of beads on the necklace. The second line is n spaces separated by a positive integer, all the numbers are not more than 1000.
The number of I is the head mark of the first bead (1≤i≤N ), when 1≤i , the tail mark of the first bead shall be equal to the head mark of the first i+1 bead. The tail mark of the nth bead should be equal to the head mark of the 1th bead. As for the order of the beads, you can be sure: put the necklace on the table, do not cross, arbitrarily specify the first bead, and then clockwise to determine the order of the other beads.
Output Format
The output file has only one row, which is a positive integer e (e≤2100000000 ), which is the total energy released for an optimal aggregation order.
Description
NOIP2006 Raise Group
Sample Input
42 3 5 10
Sample Output
710
At first did not want to understand, thought that as long as the largest energy beads to find and then in order to absorb the good, did not think not in order to absorb, but arbitrarily choose two adjacent absorption.
The concrete idea is very good understanding, is the dynamic plan, divides the current necklace into two sections, then the energy value is the maximum release energy of these two sections plus the energy of merging the two sections, the initial state is a single energy bead (releasing energy is 0). Then calculate the case of merging 2,3,...N. The more troublesome is the necklace is a ring, just at the beginning did not think carefully, see a blog solution, opened a 2n array to store data two times, but the result is wrong, because my method and the method of the blog is a little different, simply say, his method is:
The outer loop is the right boundary, then the left boundary, and then the dividing line of the merge; my outer loop is the combined interval length, then the left boundary, and then the dividing line of the merge.
This is the original error code:
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 intN;6 intnum[ the*2];7 intdp[ the*2][ the*2];8 intMain () {9 TenCin>>N; One for(inti =0; I < n;++i) { ACin>>Num[i]; -Num[i+n] =Num[i]; - } theMemset (DP,0,sizeof(DP)); - intMaxa =0; - for(inti =1; I < n;++i) { - for(intj =0; J < n;++j) { + for(intK = J;k < j+i;++k) { -Dp[j][j+i] = max (dp[j][j+i],dp[j][k]+dp[k+1][j+i]+num[j]*num[k+1]*num[j+i+1]); + } A } at } - - for(inti =0; i < N;++i)if(dp[i][i+n-1]>maxa) Maxa = dp[i][i+n-1]; -cout<<maxa<<Endl; - - return 0; in}
View
This is corrected:
1#include <iostream>2#include <cstring>3 using namespacestd;4 5 intN;6 intnum[ the*2];7 intdp[ the*2][ the*2];8 intMain () {9 TenCin>>N; One for(inti =0; I < n;++i) { ACin>>Num[i]; -Num[i+n] =Num[i]; - } theMemset (DP,0,sizeof(DP)); - intMaxa =0; - for(inti =1; i < n;++i) {//The number of combined energy beads - for(intj =0; J < N;++j) {//left Border + for(intK = J;k < J+i;++k) {//Right Border -dp[j][(j+i)%n] = max (dp[j][(J+i)%n],dp[j][k]+dp[(k +1)%n][(J+i)%n]+num[j]*num[(k +1)%n]*num[(j+i+1)%n]); + } A } at } - - for(inti =0; i < N;++i)if(Dp[i][(i+n-1)%n]>maxa) Maxa = dp[i][(i+n-1)%n]; -cout<<maxa<<Endl; - - return 0; in}
View
Recently more and more feel that they still have a long way to go, writing code is always a variety of low-level bugs, it is difficult to find code code or can not be urgent, want to clearly write but can improve efficiency.
"Shanghai Jiao Tong OJ" Energy necklace (Dynamic planning)