Energy necklace -- Combination of stones (2)

Source: Internet
Author: User

Energy necklace

[Problem 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 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 <span>, 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

 

It is similar to the combination of stones, but because it is a ring, it adopts a special processing method: copy the original array and connect it to the back of it.

Code

 1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <string. h>
4 long f [301] [302], max = 0;
5 file * In, * out;
6 int main (){
7 int I, J, K, t, a [2002], N;
8 In = fopen ("energy1.in", "R ");
9 out = fopen ("enerty. ans", "W ");
10 fscanf (in, "% d", & N );
11 For (I = 1; I <= N; I ++)
12 {
13 fscanf (in, "% d", & A [I]);
14 A [n + I] = A [I]; ---- prepare for Ring Processing
15}
16 memset (F, 0, sizeof (f ));
17 For (k = 2; k <= N; k ++)
18 For (I = 1; I <= 2 * n; I ++) ---- the cycle also needs to change accordingly.
19 {
20 t = I + k-1;
21 For (j = I; j <t; j ++)
22 if (F [I] [T] <F [I] [J] + F [J + 1] [T] + A [I] * A [J + 1] * A [t + 1])
23 f [I] [T] = f [I] [J] + F [J + 1] [T] + A [I] * A [J + 1] * [t + 1];
24}
25 For (I = 1; I <= N; I ++)
26 if (max <F [I] [I + n-1])
27 max = f [I] [I + n-1];
28
29 fprintf (Out, "% LD \ n", max );
30 fclose (in );
31 fclose (out );
32 return 0;
33}
34

 

 

 

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.