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

题解: 设dp[i][[j] 为从i到j取完出了a[i]和a[j]这两个数的最小代价。

终于所要求出的是dp[1][n]。

如果在i到j区间内最后一个取a[k],那么子问题便能够看得出来:求出dp[i][k]和dp[k][j]的最小代价。求出这两个子问题的代价再加上最后一个取出a[k]的代价,即为dp[i][j]的代价。

问题转化为求出子问题的代价,直到仅仅剩下三个数为止,三个数仅仅能取中间的数。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX=0x3f3f3f3f;
const int maxn = 100+10;
int n, dp[maxn][maxn], a[maxn];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 2; i <= n-1; i++) dp[i-1][i+1] = a[i-1]*a[i]*a[i+1]; //边界,三个数仅仅能取中间的数
for(int len = 4; len <= n; len ++)
for(int i = 1; i <= n-len+1; i++) {
int j = i+len-1;
dp[i][j] = MAX;
for(int k = i+1; k <= j-1; k++)
dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j] + a[j]*a[k]*a[i]);
}
printf("%d\n", dp[1][n]);
return 0;
}



POJ 1651 Multiplication Puzzle (区间DP)的更多相关文章

  1. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  2. POJ 1651 Multiplication Puzzle 区间dp(水

    题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...

  3. POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)

    传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K T ...

  4. Poj 1651 Multiplication Puzzle(区间dp)

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

  5. POJ 1651 Multiplication Puzzle (区间DP,经典)

    题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...

  6. POJ1651:Multiplication Puzzle(区间DP)

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

  7. poj 1651 Multiplication Puzzle【区间DP】

    题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...

  8. poj 1651 Multiplication Puzzle

    题目链接:http://poj.org/problem?id=1651 思路:除了头尾两个数不能取之外,要求把所有的数取完,每取一个数都要花费这个数与相邻两个数乘积的代价,需要这个代价是最小的 用dp ...

  9. POJ 1651 Mulitiplication Puzzle

    The multiplication puzzle is played with a row of cards, each containing a single positive integer. ...

随机推荐

  1. Iphone6手机不同浏览器页面尺寸设计

    做移动端html5页面适配,通常要考虑很多种情况. 对于同一部手机,通常要考虑如下3点: 1. 在手机普通浏览器中打开,比如Safari浏览器,UC浏览器,QQ浏览器,360浏览器,谷歌浏览器. 2. ...

  2. nginx + uswgi +django

    适合ubuntu 系统,不只是树莓派 安装必要软件 pt-get install build-essential psmisc apt-get install python-dev libxml2 l ...

  3. python开发_tkinter_获取单选菜单值

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  4. SDWebImage支持WebP格式图片

    SDWebImage本身就已经支持了webp格式的图片 1.下载libwebp https://github.com/webmproject/libwebp 然后你需要先安装好有homebrew或者m ...

  5. spring @Transactional注解无效

    <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jd ...

  6. h5的部分新元素或属性

    #h5的新元素或属性 - datalist标签(新表单元素):规定了input元素可能的选项列表.   * 条件:datalist的id属性值必须等于input元素的list属性值.          ...

  7. oracle备份恢复学习

    备份和恢复是常遇到的操作,逻辑备份和物理备份.物理备份又分为冷备份和热备份. 一.逻辑备份,恢复 是指使用exp命令,简单易行,不影响正常的数据库操作.exp   用户名/密码   file=/hom ...

  8. Apache参数的优化(转)

    按照前面提到的版本问题,Apache可以直接使用2.0版本产品线.针对Apache的优化主要是针对httpd.conf的优化,当然还有其他地方,如果特别留意的话,网上常有专家惊呼“居然这么多人忽略xx ...

  9. 用资源管理器右键编译 Visual Studio 解决方案文件

    每次改动 VC 工程之后都要重新编译,每次 VS 又会生成调试数据库文件,很费时间,于是研究了一下如何在资源管理器中直接编译,还真发现了解决办法. 以下是适用 Visual Studio 2008 的 ...

  10. 报错:未能加载文件或程序集“WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    □ 背景 通过NuGet安装某程序包后,运行程序出现如上错误.   □ 分析 可能是程序集版本不兼容引起的,可以通过NuGet先把程序包删除,然后再安装最新或某个版本的程序包.   □ 解决方法 通过 ...