Energy necklace (interval DP, ring)

Source: Internet
Author: User

On the Mars planet, every mars person carries a pair of energy necklaces with him. There are n energy beads on the necklace. An energy beads are beads with header and tail tags, which correspond to a positive integer. In addition, for the adjacent two beads, the tail mark of the previous Bead must be equal to the head mark of the next bead. Because only in this way, the two beads can be aggregated into one bead through the suction cup (which is an organ that Mars humans absorb energy, at the same time, it releases the energy that can be absorbed by the suction cup. If the head of the previous energy bead is marked as m, the tail is marked as R, the head of the next energy bead is marked as R, and the tail is marked as N, then, the energy released after aggregation is (in the unit of Mars). The head of the newly generated beads is marked as m, and the tail is marked as N.

When needed, the Mars user uses a suction disc to hold the adjacent two beads and aggregate them to obtain energy until there is only one bead left on the necklace. Obviously, the total energy produced by different aggregation sequences is different. Please design an aggregation sequence to maximize the total energy released by a chain of necklaces.

For example, set the head and tail labels of N = beads ). We use the mark "yellow" to indicate the aggregation operation of the two beads. (J yellow K) indicates the energy released after the aggregation of the J and K beads. The energy released after the aggregation of 4th and 1 beads is:

(4 rows 1) = 10*2*3 = 60.

The total energy released by this chain of necklaces is

(4 rows 1) rows 2) rows 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), indicating the number of beads on the necklace. The second row is n positive integers separated by spaces. The number of All integers cannot exceed 1000. The number of I is the head mark (1 ≤ I ≤ n) of the I beads. When I <n, the tail mark of beads I should be equal to the head mark of beads I + 1. The tail mark of N beads should be equal to the head mark of 1st beads.

As for the order of beads, you can determine the order of the beads by placing the necklace on the desktop. Do not cross the beads, specify the First beads at will, and then determine the order of other beads clockwise.

[Output file]

The output file energy. Out has only one row, which is a positive integer e (e ≤ 2.1*109) and the total energy released for an optimal aggregation order.

[Input example]

4

2 3 5 10

[Output example]

710

[Problem Analysis]

This question is probably a scoring question for this exam, and most contestants have done it. However, most people simply think about it. In fact, it is the deformation of the classic stone merger.

(1) Standard Algorithms

The test point of this question should be the dynamic planning of the interval. Before thinking about this problem, we must first solve the ring of the necklace. According to the meaning, we can enumerate cut points and process the ring into a chain. Of course, a better way is to cut the ring from any point and copy it into two chains to connect the two chains at the beginning and end. For the reading of the question, we can directly copy the read data and connect it as follows:

2 3 510 -----------> 2 3 5 10 2 3 5 10

After such processing, any chain with the length of N + 1 can represent a ring, and the problem is converted into a chain with the arbitrary length of N + 1, the maximum total energy released.

That is to say, from any point (I <k <j) the solution to splitting the chain into two segments is to merge the two segments to release the maximum energy. After the merger, the two beads are combined again to release the energy. Further decomposition of this Sub-problem is to break down the chain into 1, that is, when there are two beads, the two pillars are generated without releasing energy, the energy released by merging them is M * r * n. (This is the boundary condition ).

We designed a State opt [I, j] to indicate the maximum energy value that can be released by a chain necklace with the merging head I and the end J. The boundary condition is OPT [I, I] = 0 (1 <= I <= N * 2 ).

According to the definition, it is not difficult to obtain the dynamic state transition equation:

OPT [I, j] = max {OPT [I, j], OPT [I, K] + OPT [K, j] + A [I] * A [k] * A [J]} (I <k <j)


#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;int n, dp[100][101], a[210][3];int main(){    while(scanf("%d",&n) != EOF)    {        int i, j, k;        for(i = 0; i < n; i++)        {            scanf("%d",&a[i][0]);            if(i > 0) a[i-1][1] = a[i][0];        }        a[n-1][1] = a[0][0];        a[0][1] = a[1][0];        for(i = 0; i < n; i++)        {            a[i+n][0] = a[i][0];            a[i+n][1] = a[i][1];        }        for(k = 0; k < n; k++)        {            for(i = 0; i < 2 * n; i++)            {                for(j = i ; j < i + k; j++)                {    int tmp = dp[i][j] + dp[j+1][i+k] + a[i][0] * a[j][1] * a[i+k][1];    if(tmp > dp[i][i+k]) dp[i][i+k] = tmp;                }            }        }        int ret = 0;        for(i = 0; i < n; i++)            ret = max(ret, dp[i][i+n-1]);        printf("%d\n",ret);    }    return 0;}
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.