题目描述
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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 must contain a single integer - the minimal score.
样例输入
6 10 1 50 50 20 5
样例输出
3650
问题解决
动态规划主要是按照区间从小到大来,比如题目中给的例子,是从如下等腰直角三角形的底边一层一层往上走:
0 0 0 500 3000 3700 3650
0 0 0 0 2500 3500 3600
0 0 0 0 0 50000 17500
0 0 0 0 0 0 5000
#include <iostream>
using namespace std;
int arr[105];
int dp[105][105];
int main(){
int N;
cin>>N;
for(int i=1;i<=N;i++){
cin>>arr[i];
}
for(int len=2;len<N;len++){//区间长度
for(int i=1;i+len<=N;i++){//起始位置
int j=i+len;
dp[i][j]=10000000;
for(int k=i+1;k<j;k++){//断点位置
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+arr[i]*arr[k]*arr[j]);
}
}
}
cout<<dp[1][N]<<endl;
return 0;
}