Energy Necklace Interval DP

Source: Internet
Author: User

Energy Necklace

Time limit: 1 Sec memory limit: up to MB
Submitted by: Resolution: 15
Submitted State [Discussion Version]

Title DescriptionEach Zenith star is wearing a string of energy necklaces with n beads of energy 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, the two beads can be aggregated into a bead by the suction cup (the suction cup is an organ of the Zenith star to absorb energy), releasing the energy that can be absorbed by the suction cup. 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 (the Zenith Star Unit of measure), the head of the newly produced bead is marked M, and the tail mark is n.

When needed, the Zenith star uses a suction cup to hold the two adjacent beads together to obtain energy through 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) =10x2x3=60.

This string necklace can be obtained by an aggregation order of the optimal values, releasing the total energy of

(4⊕1) ⊕2) ⊕3) =10x2x3+10x3x5+10x5x10=710. InputThe first line 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

The output is only one row, which is a positive integer E (e≤2.1*109), releasing the total energy for an optimal aggregation order.

Sample input
42 3 5 10
Sample output
710

An array of n*2 for an annular interval DP this is actually the same as the linear interval DP.

The initial value is 0 because the same location is not merged

#include <cstring>#include<vector>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#defineFi first#defineSe Second#definePi pair<int,int>#defineMD Make_pair#defineHa pair<int,pair<int,pi> >using namespacestd;Const intinf=0x3f3f3f3f;intdp[ -][ -],num[ -],n;voidMerge_eg (intXinty) {     for(intv=1; v<n;v++){         for(intI=0;i<2*n-v-1; i++){            intj=i+v;  for(intk=i;k<j;k++) {Dp[i][j]= Max (dp[i][j],dp[i][k]+dp[k+1][j]+num[i]*num[k+1]*num[j+1]); }        }    }}intMain () {//freopen ("data.in", "R", stdin);scanf"%d",&N);  for(intI=0; i<n;i++) {scanf ("%d", num+i); Num[i+n]=Num[i]; } merge_eg (0,2*n-1); intmx=0;  for(intI=0; i<n;i++) {MX=max (mx,dp[i][i+n-1]); } printf ("%d\n", MX);}

Energy Necklace Interval DP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.