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 the 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 would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would 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
6 10 1 50 50 20 5
Sample Output
3650
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 the 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 would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would 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
6 10 1 50 50 20 5
Sample Output
3650
题意:一系列的数字,除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值,最后将所有价值加起来,要求最小值
思路:求出每个区间的最小值,一直扩散到整个区间
这题挺简单的,只是我最近没做这种题,有个地方一直回不懂,先说下怎么解霸
dp[le][ri]表示这个区间的最小值
可以选取这个区间内最后一个消掉的元素a[k],
遍历k,则dp[le][ri]=min(a[le-1]*a[k]*a[ri+1]+dp[le,k-1]+dp[k+1][ri])
只不过我一直在想,我们说的是最后一个消掉k,然后分成两端
,那么就可以先消掉[le,k-1],再消[k+1,ri],或者反过来,但是,假如说我先消掉一个
左区间的元素,再消掉一个右区间的元素,怎么办呢???
对。。。我就是纠结这个有点傻的问题纠结了半天,然而实际上。。。。
不管左右区间的操作怎么穿插,对最终结果都不会有影响,只要左区间和右区间内部操作的相对顺序
不变就行,
例如,le=1,k=3,ri=5
左区间操作:1,2 右区间操作:4,5 最后操作:3
那么:1,2,4,5,3 1,4,2,5,3 1,4,5,2,3之类的结果都是相同的