TYVJ 1056 Energy Necklace interval DP (very god)

Source: Internet
Author: User

P1056Energy Necklace Time: 1000ms/Space: 131072kib/java class name: Main background NOIP2006 raise group The first description on Mars planet, each Mars person carries a string of energy necklace. 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 format input File the first line of 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 of I is the head mark (1≤i≤n) of the first bead, when i<n< span>, 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 format output file energy.out Only one line, is a positive integer E (e≤2.1*109), the total energy released for an optimal aggregation order. Test Sample 1 input
4
2 3 5
Output
710

Test instructions: This topic differs from 1055 not in a row but in a series of end-to-end, the analysis problem is

Every time you eliminate a bead K cost is a[k-1]*a[k]*a[k+1] ask until the remaining bead price and the maximum value

Problem: This topic is very specific to see the code in the comments

In addition TYVJ Toxic Multi-group input will WA8

1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^    ^ ^5 o o6 ******************************/7 //#include <bits/stdc++.h>8#include <iostream>9#include <cstring>Ten#include <cstdio> One#include <map> A#include <algorithm> -#include <cmath> - #definell Long Long the #definePI ACOs (-1.0) - #defineMoD 1000000007 - using namespacestd; - intN; + inta[205]; - intdp[205][205]; + intMain () A { atscanf"%d",&n); -      for(intI=1; i<=n; i++)//Handling of rings -     { -scanf"%d",&a[i]); -a[i+n]=A[i]; -     } inMemset (DP,0,sizeof(DP)); -     intans=0; to      for(intgg=2*n-1; gg>=n; gg--)//GG is the upper bound of interval traversal +{//segmented segments are equivalent to all cases where a necklace split is considered -          for(intI=gg; i>=gg-n+1; i--)//The following three-layer cycle is similar to sand merging the         { *              for(intj=i+1; j<=gg; J + +) $             {Panax Notoginseng                  for(intK=i; k<j; k++)//K Note the left edge of K for the decomposition point -Dp[i][j]=max (dp[i][j],dp[i][k]+dp[k+1][j]+a[i]*a[k+1]*a[j+1]);//* Note the }//consider that the first bead May and j+1 (that is, it itself) first combine the ring apart can be imagined as the same +         } AAns=max (ans,dp[gg-n+1][gg]); the     } +printf"%d\n", ans); -     return 0; $}

TYVJ 1056 Energy Necklace interval DP (very god)

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.