# 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
`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  [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  [n]; when the disconnection point is 1 | 2, it corresponds to e_max  [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 Using namespace std;Int main (){Ifstream fin ("energy. in ");Ofstream fout ("energy. out ");Int n, e , e_max  , 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 = 1 & I-j e_max [j] [I]) e_max [j] [I] = tem;}If (e_max [j] [I]> ans) ans = e_max [j] [I];}Fout

Related Keywords:
Related Article

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.