@xunuo
2017-01-16T14:16:14.000000Z
字数 1714
阅读 1246
Time limit 1000 ms Memory limit 65536 kB
来源:
poj:poj 1651 Multiplication Puzzle
vjudge:E-Multiplication Puzzle
区间DP
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
题意:
输入第一行,有一个数n
第二行,输入n个数
先选取一个数,乘以它的前一个数和后一个数,之后将这个数去掉,再计算下一组这样的数,直至之后两个数为止,将这些数求和。
让计算出按照这种方法计算出的和的最小值,最终输出最小值
解题思路:
区间DP......
主要是找出状态转移方程,该题的状态转移方程为:
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
完整代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3ffffff
int a[110],dp[110][110];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
/*不清楚,但是感觉它要不要并没有什么区别,都可以过。。。。。不懂-_-||*/
//for(inti=0;i<n-3;i++)
// dp[i][i+2]=a[i]*a[i+1]*a[i+2];
for(int l=1;l<n;l++)
for(int i=0;i<n-l-1;i++)
{
int j=i+l+1;
dp[i][j]=inf;
for(int k=i+1;k<j;k++)
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
}
printf("%d\n",dp[0][n-1]);
}
}