Nyoj 460 necklace (interval DP)

Source: Internet
Author: User
Necklace time limit: 1000 MS | memory limit: 65535 kb difficulty: 3
Description

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
There are multiple groups of test data (<15), and each group has two rows of data. The first row of each data group 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
Corresponds to each group of data, and only one row is output, which is a positive integer e (e ≤ 2.1*10 ^ 9), the total energy released for an optimal aggregation order
Sample Input
42 3 5 10
Sample output
710
Explanation of the question: This question is very similar to this question (poj 1651), but it turns into a loop and removes one more question than that, therefore, we can combine the ring as two strings, increase the interval by a factor larger than before, and traverse it again to find the maximum value;
AC code:
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # include <queue> # include <string> # include <cmath> using namespace STD; const int INF = 1e8; int A [220]; int DP [210] [210]; int main () {int n, m; while (CIN> m) {int ans = 0; For (INT I = 1; I <= m; I ++) // directly becomes a long string, {scanf ("% d ", & A [I]); A [I + M] = A [I];} n = m * 2; // write the code according to the code that is not a ring; for (INT I = n-2; I> = 1; I --) // traverse all intervals backwards; {for (Int J = I + 2; j <= N; j ++) {DP [I] [J] = 0; // start initialization all to the minimum value; For (int K = I + 1; k <= J-1; k ++) DP [I] [J] = max (DP [I] [J], A [I] * A [k] * A [J] + dp [I] [k] + dp [k] [J]);} for (INT I = 1; I <= m; I ++) {If (DP [I] [I + M]> ans) ans = DP [I] [I + M]; // The interval must be larger, because only one is removed;} printf ("% d \ n ", ans);} return 0 ;}

 

Nyoj 460 necklace (interval DP)

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.