每日一题----POJ1651Multiplication Puzzle

原题:  

                                                      Multiplication Puzzle

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 19683Accepted: 12070

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

翻译:

乘法拼图
时限:1000毫秒内存限制:65536K
提交总数: 19683接受: 12070

描述

乘法拼图是用一排牌玩的,每张牌包含一个正整数。在移动过程中,玩家从行中取出一张牌,得分等于所取牌上的数字与左侧和右侧牌上的数字的乘积。不允许取出行中的第一张和最后一张牌。最后一步之后,行中只剩下两张牌。

目标是按最小化得分总数的顺序取牌。

例如,如果行中的牌包含数字 10 1 50 20 5,则玩家可能会拿一张带有 1、20 和 50 的牌,得分

10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000


如果他以相反的顺序拿牌,即 50,然后是 20,然后是 1,得分将是

1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150。

输入

输入的第一行包含牌数 N (3 <= N <= 100)。第二行包含 N 个介于 1 到 100 之间的整数,用空格分隔。

输出

输出必须包含单个整数 - 最小分数。

示例输入

<span style="color:#000000">6
10 1 50 50 20 5
</span>

示例输出

<span style="color:#000000">3650</span>

 思路:

这是一个动态规划问题。我们可以定义一个二维数组 dp,其中 dp[i][j] 表示从第 i 张牌到第 j 张牌之间的最小得分总数。

首先,对于只有两张牌的情况,dp[i][i+1] 可以直接计算得到,即 dp[i][i+1] = 0。

然后,对于有三张及以上的情况,我们需要用到状态转移方程。设 k 为取出的牌,k 的范围为 [i+1, j-1]。可以得到以下状态转移方程:

dp[i][j] = min(dp[i][k] + dp[k][j] + nums[i-1] * nums[k] * nums[j])

其中,nums 是输入的牌的序列。

具体的实现步骤如下:

  1. 定义一个二维数组 dp,大小为 N x N。
  2. 初始化 dp[i][i+1] 为 0。
  3. 使用两层循环,外层循环控制区间长度,内层循环控制起点位置。
  4. 在内层循环中,根据状态转移方程更新 dp[i][j]。
  5. 最终结果为 dp[1][N]。

如何写代码:

使用动态规划解决乘法拼图问题的代码实现。代码中使用了一个二维数组 dp 来存储最小得分总数。

具体的实现步骤如下:

  1. 定义一个一维数组 p,用于存储输入的牌的序列。
  2. 定义一个二维数组 dp,大小为 maxn x maxn
  3. 使用两层循环,外层循环控制区间长度 d,内层循环控制起点位置 i
  4. 在内层循环中,根据状态转移方程更新 dp[i][j]
    • 首先,初始化 dp[i][j] 为 dp[i+1][j] + p[i-1] * p[i] * p[j],即不取出第 i 张牌的得分。
    • 然后,通过遍历一个中间位置 k,计算取出第 i 张牌的得分,即 dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j]
    • 最终,将上述两种情况的较小值赋给 dp[i][j]
  5. 最终结果为 dp[1][n],其中 n 是输入的牌数。

代码:

#include<iostream>
using namespace std;
const int maxn = 101; 

int p[maxn], dp[maxn][maxn];

int solve(int n) {
    for(int d=2; d<=n; d++) {  // 区间长度
        for(int i=1; i<=n-d+1; i++) {  // 起点位置
            int j = i + d - 1;
            dp[i][j] = dp[i+1][j] + p[i-1] * p[i] * p[j];  // 不取出第 i 张牌的得分
            for(int k=i+1; k<j; k++) {  // 遍历中间位置
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j]);  // 取出第 i 张牌的得分
            }
        }
    }
    return dp[1][n];
}

int main() {
    int n;
    scanf("%d", &n);
    for(int i=0; i<n; i++) {
        scanf("%d", &p[i]);
    }
    printf("%d", solve(n-1));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

善程序员文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值