Multiplication puzzle
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:6292 |
|
Accepted:3814 |
Description
The multiplication puzzle is played with a row of cards, each containing a single positive integer. during the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on right of it. it is not allowed to take out the first and the last card in the row. after the final move, only two cards are left in the row.
The goal is to take cards in such order as to minimize the total number of scored points.
For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring
10*1*50 + 50*20*5 + 10*50*5 = 500 + 5000 + 2500 = 8000
If he wocould take the cards in the opposite order, I. e. 50, then 20, then 1, the score wocould be
1*50*20 + 1*20*5 + 10*1*5 = 1000 + 100 + 50 = 1150.
Input
The first line of the input contains the number of cards N (3 <= n <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.
Output
Output must contain a single integer-the minimal score.
Sample Input
610 1 50 50 20 5
Sample output
3650
Source
Northeastern Europe 2001, far-Eastern subregion this question is the same as the multiplication Game On tyvj, so I moved the question over there.
Question:The multiplication game is played on a row of cards. Each card contains a positive integer. In each movement, the player takes out a card, and the score is multiplied by its number on the left and right. Therefore, 1st cards and the last card are not allowed. After the last move, there are only two cards left. Your goal is to minimize the sum of scores.
For example, if the number is 10 1 50 20 5, take 1, 20, 50 in sequence, and the total score is 10*1*50 + 50*20*5 + 10*50*5 = 8000
Take 50, 20, 1, and the total score is 1*50*20 + 1*20*5 + 10*1*5 = 1150. The first line of the input file in inputformat contains the number of cards (3 <= n <= 100), and the second line contains N 1-100 integers separated by spaces. Output Format outputformat output file has only one number: The minimum score of the first use of _ int64 storage, and then calculate the data range, assuming all are 100 then (n-2) + (100*100*100) = 98000000 there is no super int, so you can use Int. The first thought was to see if it was greedy and the product was the smallest each time, however, the following example cannot be used. Later, I thought that the explanation of interval DP in the Forum is very good. I will go here: the leftmost and rightmost cards cannot be removed. All cards except the two cards must be removed. The last card has a score that is only related to it and the value of the leftmost and rightmost card. This score has nothing to do with other cards. When the last card to be removed is determined (assuming the position is K ), when all cards between the leftmost end and K are taken away, the score must be related only to the cards between them, thus being independent from the cards between J and rightmost. In this way, two independent subintervals are formed, and the subintervals overlap. The solution is to take the score of the last card and the minimum score of the two subintervals. Assume that the current interval is [I, j], at (I, j) enumerate the last card to be removed, and obtain the optimal solution of the current interval through the optimal sub-problem:
DP [I] [J] = min {DP [I] [k] + dp [k] [J] + A [I] * A [J] * A [k] (I + 1 <= k <= J-1 )}
1 # include <stdio. h> 2 # include <iostream> 3 # include <string. h> 4 # include <string> 5 # include <stdlib. h> 6 # include <algorithm> 7 using namespace STD; 8 int DP [110] [110]; 9 int A [110]; 10 const int INF = 0x3f3f3f; 11 int main () 12 {13 int I, J, K, P, n = 0; 14 While (scanf ("% d", & N )! = EOF) 15 {16 for (I = 0; I <n; I ++) 17 scanf ("% d", & A [I]); 18 memset (DP, 0, sizeof (DP); 19 for (P = 3; P <= N; P ++) // The Interval Length is 20 {21 for (I = 0; I <N-2; I ++) // enumeration interval start point 22 {23 J = I + p-1; // Interval End Point 24 DP [I] [J] = inf; 25 For (k = I + 1; k <j; k ++) 26 dp [I] [J] = min (DP [I] [k] + dp [k] [J] + A [I] * A [k] * A [J ], DP [I] [J]); 27} 28} 29 printf ("% d \ n", DP [0] [n-1]); 30} 31 return 0; 32}View code