1640: Energy necklace, 1640 energy necklace

Source: Internet
Author: User

1640: Energy necklace, 1640 energy necklace
1640: Energy necklace

Time Limit: 1 Sec memory limit: 128 MB
Submitted: 34 solution: 24
[Submit] [Status] [discussion version] 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 m * r * n (Mars Unit). 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. Then the energy released after the aggregation of 4th and 1 beads is: (4 Tib 1) = 10*2*3 = 60. The total energy released by this chain of necklaces in an aggregation order of the optimal values is (4 then 1) then 2) then 3) = 10*2*3 + 10*3*5 + 10*5*10 = 710. The first line 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. The output contains only one row, which is a positive integer E (E ≤ 2.1*10 ^ 9) and the total energy released for an optimal aggregation order. Sample Input
4             2 3 5 10
Sample output
710
Prompt Source

 

 

Analysis:

DP is similar to the combination of stones and matrix concatenation.

First, the necklace is ring-shaped. We must disconnect it from somewhere. Where should we break it? Enumeration. When we have disconnected it from somewhere, the split beads are marked as 1-n in order, at this time, if we use e_max [I] [j] to represent the maximum energy that can be obtained from the combination of the I beads to the j beads, the request is e_max [1] [n]. If e [I] is used to indicate the header marker of the beads, it is easy to find the state transition equation: e_max [I] [j] = max (e_max [I] [k] + e_max [k + 1] [j] + e [I] * e [k + 1] * e [j + 1]), where, I <= k <j.

In this way, the enumerated disconnection points, I, j, and k, and the complexity is O (n ^ 4 ). This may cause timeout for running limit data for n <= 100 of data. Can the complexity be reduced?

We can copy e [] when reading data, such as fin> e [I]; e [I + n] = e [I]. In this way, we do not need to enumerate the disconnection points, because when the disconnection point is n | 1, it corresponds to e_max [1] [n]; when the disconnection point is 1 | 2, it corresponds to e_max [2] [n + 1], and so on. In this way, the complexity of the algorithm is reduced to O (n ^ 3), and all data with n <= 100 can be passed.

In addition, through the state transition equation, we can find that e_max [I] [j] is transferred from e_max [I] [k] And e_max [k + 1] [j, because k <j, j <= j, I> = I, k + 1> I, therefore, it is much easier to write a program from small to large enumeration j to arrogant to small enumeration I.

The source code is as follows:

# Include <fstream>

Using namespace std;

Int main (){
Ifstream fin ("energy. in ");
Ofstream fout ("energy. out ");
Int n, e [201], e_max [201] [201], ans = 0;
Fin> n;
For (int I = 1; I <= n; I ++ ){
Fin> e [I];
E [I + n] = e [I];
}
Memset (e_max, 0, sizeof (e_max ));
For (int I = 2; I <n + n; I ++) for (int j = I-1; j> = 1 & I-j <n; j --){
For (int k = j; k <I; k ++ ){
Int tem = e_max [j] [k] + e_max [k + 1] [I] + e [j] * e [k + 1] * e [I + 1];
If (tem> e_max [j] [I]) e_max [j] [I] = tem;
}
If (e_max [j] [I]> ans) ans = e_max [j] [I];
}
Fout <ans <endl;
Fin. close ();
Fout. close ();
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.