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;//结束
}

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

  1. POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP

    题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65 ...

  2. POJ1651:Multiplication Puzzle(区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...

  3. POJ1651:Multiplication Puzzle(区间dp)

    Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9419 Accepted: 5850 ...

  4. poj1651 Multiplication Puzzle

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

  5. POJ1651 Multiplication Puzzle【区间DP】

    LINK 每次删除一个数,代价是左右两边相邻的数的当前数的积 第一个和最后一个数不能删除 问最后只剩下第一个数的最后一个数的最小代价 思路 很简单的DP 正着考虑没有办法确定两边的数 那么就把每个区间 ...

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

    http://blog.csdn.net/libin56842/article/details/9747021 http://www.cnblogs.com/devil-91/archive/2012 ...

  7. poj1651 Multiplication Puzzle(简单区间dp)

    题目链接:http://poj.org/problem?id=1651 题意:一系列的数字,除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值, 最后将所有价值加起来,要求最小值. ...

  8. POJ1651 Multiplication Puzzle (区间DP)

    这道题的妙处在于把原问题看成矩阵连乘问题,求这些矩阵相乘的最小乘法次数,比如一个i*k矩阵乘一个k*j的矩阵,他们的乘法次数就是i*k*j (联想矩阵乘法的三层循环),题目说的取走一张牌,类似于矩阵相 ...

  9. ZOJ 1602 Multiplication Puzzle(区间DP)题解

    题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...

随机推荐

  1. yolov3 进化之路,pytorch运行yolov3,conda安装cv2,或者conda安装找不到包问题

    yolov3 进化之路,pytorch运行yolov3,conda安装cv2,或者conda安装找不到包问题 conda找不到包的解决方案. 目前是最快最好的实时检测架构 yolov3进化之路和各种性 ...

  2. D - Three Integers

    https://codeforces.com/contest/1311/problem/D 本题题意:给出a,b,c三个数,a<=b<=c: 可以对三个数中任意一个进行+1或-1的操作: ...

  3. SpringBoot学习- 5、整合Redis

    SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...

  4. c++踩坑大法好 赋值和指针的区别

    1,先说结论: 两个指针指向同一个结构,一个改了结构,另一个也会改掉. 两个指针指向同一个结构,修改了其中一个的指向,并且改了其中的内容,另一个不为所动. 2,看例子 main.cpp #includ ...

  5. js--javascript学习

    js -- javascript ECMAscript5 ECMAscript6 -- vue.js react .. 由三个部分组成 1 ECMAscript5的核心 js语言 2 BOM 浏览器对 ...

  6. C语言 malloc函数

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.                                                 ...

  7. wcf编程资料

    如下为WCF编辑资料 链接:https://pan.baidu.com/s/1kZnc6eNOfEggHSfJNXj8Ag 提取码:gj7s 复制这段内容后打开百度网盘手机App,操作更方便哦 第01 ...

  8. 小匠第二周期打卡笔记-Task03

    一.过拟合欠拟合及其解决方案 知识点记录 模型选择.过拟合和欠拟合: 训练误差和泛化误差: 训练误差 :模型在训练数据集上表现出的误差, 泛化误差 : 模型在任意一个测试数据样本上表现出的误差的期望, ...

  9. linq和匿名方法、委托、匿名委托、lambda

    委托相当于JavaScript中的闭包,c++中的函数指针. c#为了引进这个函数指针,将其进行包装成“委托”,同时将非托管的变成托管的. 1.最初的委托该怎么用 弊端:写的代码量过多,还要写一个显示 ...

  10. C++-函数与指针的关系(回调函数)

    1.函数类型 C语言中的函数有自己特定的类型 函数的类型由返回值,参数类型和参数个数共同决定 ★ int add(int i, int j)的类型为int(int, int) C语言中通过typede ...