Multiplication Puzzle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7947 | Accepted: 4912 |
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.
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
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
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
题意:
给出一个数组a,可以将其中除了头尾两个数之外的任何一个数字a[i]取出数列,需要的花费为 a[i-1] * a[i] * a[i+1],问如果需要把这个数列除了头尾之外的数字都取完,怎样的取出顺序是花费最小的,输出这个最小花费
思路:
首先我们可以考虑最小的一个可以操作的区间为3个数字,是可以直接算出答案的。
对于长度小于3的区间,我们可以直接得到 dp[i][i+1] = 0;
对于长度大于3的区间,我们可以在其中找到一个中点,例如区间 i ~ j 取中点k 即分为区间 i~k 和 k~j, 对于区间 i~k 我们有答案 dp[i][k] 对于区间 k~j 我们有答案 dp[k][j] ,而在这两个区间运算完之后 会剩下的是 a[i],a[k],a[j],所以为了消除k我们会有花费 a[i] * a[k] * a[j],即转移方程 :
dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j] + a[i]*a[k]*a[j]);
#include"iostream"
#include"cstring"
#include"cstdio"
#include"algorithm"
#define INF 0x7f7f7f7f
using namespace std;
int m;
int a[105];
int dp[105][105];
int main(void)
{
while(~scanf("%d",&m))
{
for(int i = 0;i < m;i++) scanf("%d",&a[i]);
memset(dp,INF,sizeof(dp));
for(int i = 0;i < m-1;i++)
{
dp[i][i+1] = 0;
}
for(int l = 3;l <= m;l++)
{
for(int i = 0;i + l - 1 < m;i++)
{
for(int k = i+1;k < i + l - 1;k++)
{
dp[i][i+l-1] = min(dp[i][i+l-1],dp[i][k] + dp[k][i+l-1] + a[i]*a[k]*a[i+l-1]);
}
}
}
printf("%d\n",dp[0][m-1]);
}
return 0;
}