Question Portal: Enter
Analysis:
This question is the deformation of "Stone merge. Dynamic Planning is also used for solution.
Use dp [I, j] to represent the minimum amount of smoke released by the j-stack mixture merger starting from heap I
Sum [I, k] indicates the color of the combined j heap mixture starting from heap I.
Obviously there is a state transition equation:
Dp [I, j] = min {dp [I, k] + dp [I + k, j-k] + sum [I, k] * sum [I + k, j-k]} (1 <= k <j)
It is not difficult to write the code:
01 # include <iostream>
02 # include <cstring>
03 # include <climits>
04 using namespace std;
05
06 int main (int argc, char * argv [])
07 {
08 ios: sync_with_stdio (false );
09 int I, j, k, m, temp;
10 int n;
11 int mix [101];
12 int dp [1, 101] [2, 101];
13 int sum [101] [101];
14
15 while (cin> n)
16 {
17 memset (mix, 0, sizeof (mix ));
18 memset (dp, 0, sizeof (dp ));
19 memset (sum, 0, sizeof (sum ));
20 for (I = 1; I <= n; ++ I)
21 {
22 cin> mix [I];
23 sum [I] [1] = mix [I];
24 dp [I] [1] = 0;
25}
26 for (I = 1; I <= n; ++ I)
27 {
28 for (j = 2; I + j <= n + 1; ++ j)
29 {
30 sum [I] [j] = (sum [I] [j-1] + mix [I + j-1]) % 100;
31}
32}
33
34 for (I = n; I> = 1; -- I)
35 {
36 for (j = 2; I + j <= n + 1; ++ j)
37 {
38 m = INT_MAX;
39 for (k = 1; k <j; ++ k)
40 {
41 temp = dp [I] [k] + dp [I + k] [j-k] + sum [I] [k] * sum [I + k] [j- k];
42 if (temp <m)
43 m = temp;
44}
45 dp [I] [j] = m;
46}
47}
48 cout <dp [1] [n] <endl;
49}
50 return 0;
51}