题解【POJ1651】Multiplication Puzzle

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

Solution

题意简述:给定一个序列,取走一个数的代价是它乘上它相邻的两个数,两头的数不能取,求最小代价。

考虑区间DP。

令\(dp[i][j]\)表示把\(i+1\)~\(j-1\)的数都取走的最小代价。

然后进行区间DP,注意区间的长度的范围是\(3\)~\(n\)。

状态转移方程:

\[dp[i][j]=min(dp[i][j], dp[i][k] + dp[k][j] + a[i] * a[j] * a[k])\]

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>

using namespace std;

inline int gi()
{
    int f = 1, x = 0; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
    return f * x;
}

int n, m, dp[103][103], a[103];

int main()
{
    memset(dp, 0, sizeof(dp));//初始化
    n = gi();
    for (int i = 1; i <= n; i++) a[i] = gi();
    for (int i = 3; i <= n; i++)//枚举区间长度
    {
        for (int j = 1; j + i <= n + 1; j++)//枚举起点
        {
            int k = j + i - 1;//终点
            dp[j][k] = 1000000007;//当前区间的dp数组初始化
            for (int l = j + 1; l <= k - 1; l++)//枚举分割点
            {
                dp[j][k] = min(dp[j][k], dp[j][l] + dp[l][k] + a[j] * a[k] * a[l]);//进行状态转移
            }
        }
    }
    printf("%d\n", dp[1][n]);//最后输出答案
    return 0;//结束
}

原文地址:https://www.cnblogs.com/xsl19/p/11125707.html

时间: 2024-09-29 09:41:24

题解【POJ1651】Multiplication Puzzle的相关文章

POJ1651——Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: 3964 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

POJ1651 Multiplication Puzzle 区间dp

题意:给一个序列,每次选一个数(记为b),计算左边离他最近的数(记为a),右边离他最近的数(记为c),计算a*b*c,sum+=a*b*c 重复操作直到剩下开头结尾两个数 不同的方案对应不同的sum 计算最小的sum值 分析:典型的区间dp,dp[i][j]表示把从i到j所有的数都选走得到的最小值 dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]+num[k]*num[i-1]*num[j+1]): 注:记得预处理dp[i][i],i(2~n-1) #inc

poj1651 Multiplication Puzzle(简单区间dp)

题目链接:http://poj.org/problem?id=1651 题意:一系列的数字,除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值, 最后将所有价值加起来,要求最小值. 这题容易会想到贪心就是先把最大的数先取出这样就能满足剩下的总价值尽可能的小,如果出现多个一样 的数时优先取走价值小的,但是如果有出现多个价值一样的话就不好处理了. 于是可以考虑一下用区间解决,区间转移大致是这样的 dp[j][j + i] = min(dp[j][j + i] , dp[j][k]

POJ1651:Multiplication Puzzle(区间DP 最优矩阵链乘)

题意:除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值,最后将所有价值加起来,要求最小值 和最优矩阵链乘模型一样,最后取出的数决定了序,如果没学过最优矩阵连乘找重复子问题还是比较难找的 DP //180K 0MS #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int dp[110][110];

POJ1651 Multiplication Puzzle(相邻乘积之和最小,区间DP)

http://blog.csdn.net/libin56842/article/details/9747021 http://www.cnblogs.com/devil-91/archive/2012/06/26/2562976.html #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include &l

poj1651 Multiplication Puzzle

比较特别的区间dp.小的区间转移大的区间时,也要枚举断点.不过和普通的区间dp比,断点有特殊意义.表示断点是区间最后取走的点.而且一个区间表示两端都不取走时中间取走的最小花费. #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstr

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move

xtu read problem training 4 B - Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 165164-bit integer IO format: %lld      Java class name: Main The multiplication puzzle is played with a row of cards, each containing a s

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (