[]vijos & Codevs Energy Necklace

Source: Internet
Author: User

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.

formatInput format

The first line of the input file 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 1≤i

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≤2.1*109), releasing the total energy for an optimal aggregation order.

Example 1Sample Input 1
42 3 5 10
Sample Output 1
710

Limit

1s

Source

NOIP2006 first question

(Turn from Vijos1312, topic portal [codevs]&[Vijos])

This problem and the addition of two fork tree a bit like, so immediately think of the interval DP. The difference between them is probably in one tree and one on the ring. Processing rings are treated like knights, broken loops into chains, and special treatments at both ends. Then use F[aflag][i][j] to carry out DP.

(PS, the interval below refers to a section on the necklace, [I, j] represents the first energy bead to the J energy Bead This paragraph)

Where Aflag is 0, the maximum value of the energy beads in the chain,[I, J] interval is combined. When the Aflag is 0, the open point is indicated, from J to I (Interval [j, N] and [1, I]).

Relatively simple on the chain, it is easy to think of equations f[0][i][j] = Max{f[0][i][k] + f[0][k + 1][j] + a[i] * a[k + 1] + a[(j + 1)% n]}(a indicates the number of n read, subscript from 0 start)

There are three things you can do.

    1. K on the left side,f[1][i][j] = Max{f[0][k][i] + f[1][k-1][j] + a[(i + 1)% n] * a[k] * A[j]}

    2. K at right end,f[1][i][j] = Max{f[0][j][k] + f[1][i][k + 1] + a[j] * a[k + 1] * a[(i + 1)% n]}

    3. Just two bands in the chain (interval [j, N] and [1, I]) merge,Smax (F[1][i][j], f[0][0][i] + f[0][j][n-1] + a[j] * a[0] * a[i + 1] )

Finally, find the maximum value in f[0][0][n-1] and f[1][i][i + 1] .

Code
1 /**2 * Codevs & vijos.org3 * problem#1154 & 13124 * Accepted & Accepted5 * TIME:13MS & 30ms6 * memory:364k & 536k7  */ 8#include <iostream>9#include <sstream>Ten#include <cstdio> One#include <cmath> A#include <cstdlib> -#include <cstring> -#include <cctype> the#include <queue> -#include <Set> -#include <map> -#include <stack> +#include <vector> -#include <algorithm> + using namespacestd; AtypedefBOOLBoolean; at #defineSmin (A, B) (a) = min ((a), (b)) - #defineSmax (A, B) (a) = Max ((a), (b)) -Template<typename t> -InlinevoidReadinteger (t&u) { -     Charx; -     intAflag =1; in      while(!isdigit ((x = GetChar ())) && x! ='-'); -     if(x = ='-'){ toAflag =-1; +x =GetChar (); -     } the      for(U = x-'0'; IsDigit ((x = GetChar ())); u = u *Ten+ X-'0'); * ungetc (x, stdin); $U *=Aflag;Panax Notoginseng } -  theTemplate<typename t>classmatrix{ +      Public: AS Mp; the         intlines; +         introws; - Matrix ():p (NULL) {} $Matrix (intRowsintlines): Lines (lines), rows (rows) { $p =Newt[(lines *rows)]; -         } -t*operator[](intPOS) { the             return(P + pos *lines); -         }Wuyi }; the #defineMatset (M, I, s) memset ((m). P, (i), (s) * (m). Lines * (m). Rows) -  Wu #defineIDX (i) a[(i + N)% n] -  About intN; $ int*A; -matrix<int> f[2]; -  -Inlinevoidinit () { A Readinteger (n); +f[0] = matrix<int>(n, n); thef[1] = matrix<int>(n, n); -A =New int[(Const int) (n)]; $      for(inti =0; I < n; i++) the Readinteger (A[i]); the } the  theInlinevoidsolve () { -Matset (f[0],0xf0,sizeof(int)); inMatset (f[1],0xf0,sizeof(int)); the      for(inti =0; I < n-1; i++){ thef[0][i][i +1] = a[i] * a[i +1] * IDX (i +2); About     } the      for(inti =0; I < n; i++) f[0][i][i] =0; thef[1][0][n-1] = A[n-1] * a[0] * a[1]; the      for(ints =2; s < n; s++){ +          for(inti =0; i + S < n; i++){ -             intj = i +s; the              for(intK = i; K < J; k++){BayiSmax (f[0][I][J], f[0][I][K] + f[0][k +1][J] + a[i] * a[k +1] * IDX (j +1)); the             } the         } -          for(inti =0; I-s <0; i++){ -             intj = (i-s + N)%N; the              for(intK = i; K >0; k--){ theSmax (f[1][I][J], f[0][k][i] + f[1][k-1][J] + idx (i +1) * A[k] *a[j]); the             } the              for(intK = J; K < n-1; k++){ -Smax (f[1][I][J], f[0][J][K] + f[1][i][k +1] + a[j] * a[k +1] * IDX (i +1)); the             } theSmax (f[1][I][J], f[0][0][i] + f[0][j][n-1] + a[j] * a[0] * a[i +1]); the         }94     } the     intresult = f[0][0][n-1]; the      for(inti =0; I < n-1; i++){ theSmax (Result, f[1][i][i +1]);98     } Aboutcout <<result; - }101 102 ///Main Funtion103 intMainintargcChar*argv[]) {104 init (); the solve ();106     return 0;107}

[]vijos & Codevs Energy Necklace

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.