POJ 1651 Multiplication Puzzle (区间dp 矩阵连乘)

Multiplication Puzzle
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6275
Accepted: 3808

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

Source

Northeastern Europe 2001, Far-Eastern Subregion


题目链接  :http://poj.org/problem?id=1651

题目大意  :给一串数,规定不能取头和尾,取出的数的得分为其乘其左边和右边的数的积,一直取到还剩两个数,求最小总得分

题目分析  :典型的区间dp,在网上看到很多博客对这题基本上都是贴个代码,或者粗略的讲个思路,笔者愚钝,自己想了好久才想明白,在这里详细道来,这题可以转化成矩阵连乘的问题(其实两个问题的dp方程几乎相同),定义dp[i][j](j-i>=2)为序列num[i..j]在该规则下的最优解,可选的值为num[i+1]到num[j-1],(注意这里 num[i]和num[j]是不可取的) 我们取一个k(i < k <j),则取出k时的得分为num[i]*num[k]*num[j],则此时dp[i][j] = dp[i][k] + dp[k][j] + num[i]*num[k]*num[j]相当于这样一个序列( i ____ k ____ j )下划线部分为dp[i][k]和dp[k][j],因为很难从原序列得到答案,因此我们从只有两个数的序列往后递推(当只有3个数时,下划线部分相当于没有数字),因此此时下划线部分的最优值已经得到,再次强调这里的k的范围是(i < k < j)所以此时dp[i][j] = min(dp[i][k] + dp[k][j] +num[i]*num[k]*num[j])  (i+1<=k<=j-1) , 妹子问为什么不能用贪心每次取当前的最大值,给组数据1 2 3 2 1,妹子无语了.


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
using namespace std;
int const MAX = 101;
int num[MAX];
int dp[MAX][MAX]; 

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        for(int a = 1; a <= n; a++)
            scanf("%d",&num[a]);
        memset(dp,0,sizeof(dp));
        //从2开始枚举长度(也可以理解为i和j的差值)
        for(int len = 2; len < n; len++) 
        {
            //这里不能写成小于n,否则得到的就是1到n-1的最优解了
            //结果一定是0,因为dp数组初始化为0
            for(int i = 1; i + len <= n; i++)
            {
                int j = i + len; //这样写是为了使j >= i+ 2
                dp[i][j] = INT_MAX;  //初始化dp数组
                //在i+1到j-1中枚举k的值,找到各个部分的最优子结构
                for(int k = i + 1; k <= j - 1; k++) 
                    dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + num[i]*num[k]*num[j]);
            }
        }
        //最终dp[1][n]就表示1-n这整个序列得到的最优解
        printf("%d\n",dp[1][n]);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值