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
 
题意:一系列的数字,除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值,最后将所有价值加起来,要求最小值
思路:求出每个区间的最小值,一直扩散到整个区间
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int n,a[105],dp[105][105],i,j,k,l; int main()
{
while(~scanf("%d",&n))
{
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(l = 2; l<n; l++)//长度从2开始枚举
{
for(i = 2; i+l<=n+1; i++)
{
j = i+l-1;
dp[i][j] = 100000000;
for(k = i; k<j; k++)//枚举中点
dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+a[i-1]*a[k]*a[j]);
}
}
printf("%d\n",dp[2][n]);
} return 0;
}

POJ1651: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. POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP

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

  4. POJ1651:Multiplication Puzzle(区间dp)

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

  5. [ZOJ]3541 Last Puzzle (区间DP)

    ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one ...

  6. POJ1651Multiplication Puzzle[区间DP]

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

  7. poj1651 Multiplication Puzzle

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

  8. POJ1651 Multiplication Puzzle【区间DP】

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

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

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

随机推荐

  1. Mapreduce执行过程分析(基于Hadoop2.4)——(三)

    4.4 Reduce类 4.4.1 Reduce介绍 整完了Map,接下来就是Reduce了.YarnChild.main()—>ReduceTask.run().ReduceTask.run方 ...

  2. linux进程控制命令

    & 加在一个命令的最后,可以把这个命令放到后台执行 ,如gftp &. ctrl + z 可以将一个正在前台执行的命令放到后台,并且处于暂停状态,不可执行. jobs 查看当前有多少在 ...

  3. 【转】B树、B-树、B+树、B*树

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  4. mvn安装jar文件到本地

    mvn install:install-file -DgroupId=com.jfinal -DartifactId=jfinal -Dversion=2.3 -Dpackaging=jar -Dfi ...

  5. ubuntu14.04恢复系统默认中文字体

    今天 Ubuntu14.04已发布就进行了更新,在配置过程中,无意安装了某些中文字体,导致系统的中文字体极其难看,根据网上说的修改配置文件和tweak 修改的方法都不能解决,最终找到的解决办法(htt ...

  6. MSSQL手札三 MSSQL存储过程

    --存储过程完成一段sql代码的封装 create proc trim --参数列表,多个间用逗号分隔 ) as --自定义代码段 ) set @str1=LTRIM(RTRIM(@str)) pri ...

  7. emWin(ucGui) Edit控件数值模式 ——符号编辑 worldsing

    emWin(ucGui) Edit控件数值模式出现负数值编辑时,如果键盘按键全可以设置独立的"-","+"键,这样可以正常编辑正数和负数,但是要没有设置这两个键 ...

  8. C# 扩展方法奇思妙用高级篇六:WinForm 控件选择器

    在Web开发中,jQuery提供了功能异常强大的$选择器来帮助我们获取页面上的对象.但在WinForm中,.Net似乎没有这样一个使用起来比较方便的选择器.好在我们有扩展方法,可以很方便的打造一个. ...

  9. thinkphp显示重复两次

    在index.php中去掉 //App::run(); 这种方式是原来的旧版本的方式,现有版本的Thinkphp已经改变,不需要输入此行即可运行.

  10. ELF学习--可执行文件

    承接上一篇.我们来看看可执行文件和重定位文件的不同点. readelf -h test 相比较重定位文件,可执行文件的ELF header中入口地址是0x8048320.而且除了section hea ...