"Problem 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 after aggregation is (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 is:
(4⊕1) =10*2*3=60.
This string necklace can be obtained by an aggregation order of the optimal values, releasing the total energy of
((4⊕1) ⊕2) ⊕3) =10*2*3+10*3*5+10*5*10=710.
"Input File"
The first line of the input file energy.in is a positive integer N (4≤n≤100) that represents 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 I is the head mark (1≤i≤n) of the first bead, when i<n, the tail mark of the first bead shall be equal to the head mark of the 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 File"
The output file Energy.out has only one row, which is a positive integer E (e≤2.1*109), which is the total energy released for an optimal aggregation order.
"Input Sample"
4
2 3 5 10
"Output Example"
710
The amount of ... is obviously an interval DP problem, the interval DP will write it ... But mentally retarded, I thought the combined head tag was the combined energy at first ... Sure enough, I'm still too naive.
1#include <cstdio>2#include <cmath>3#include <cstring>4#include <cstdlib>5#include <queue>6#include <stack>7#include <vector>8#include <iostream>9#include"algorithm"Ten using namespacestd; OnetypedefLong LongLL; A Const intmax= the; - intN; - intA[max]; the intf[max*2][max*2]; - voidinit () { - inti,j; -scanf"%d",&n); +Memset (F,0,sizeof(f)); - for(i=1; i<=n;i++) +{scanf ("%d", A +i); Aa[i+n]=A[i]; at } -a[0]=a[n],a[2*n+1]=a[0]; - } - intMain () { -Freopen ("energy.in","R", stdin); -Freopen (" Energy. out","W", stdout); inInit ();intI,j,k,len; - for(len=2; len<=n;len++) to for(i=1; i<=2*n-len+1; i++) +{j=i+len-1; - for(k=i;k<j;k++) theF[i][j]=max (f[i][j],f[i][k]+f[k+1][j]+a[i]*a[k+1]*a[j+1]); * } $ intAns0);Panax Notoginseng for(i=1; i<=n;i++) -Ans=max (ans,f[i][i+n-1]); theprintf"%d", ans); + return 0; A}
NOIP2006 Energy Necklace (interval DP)