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

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
 
题解:
区间dp。。。设dp[l][r]表示区间[l,r]的最优解,则状态转移如下:
1、当r-l=2时,也即只有三个数时,显然dp[l][r] = a[l]*a[l+1]*a[r];
2、当r-l>2时,对区间的最后一个被拿走的数进行枚举,则dp[l][r] = min(dp[l][r], dp[l][i]+dp[i][r]+a[l]*a[i]*a[r]),其中l<i<r。
 
#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<deque>
#include<algorithm>
#include<string>
#include<stack>
#include<cmath>
using namespace std;
int ans,n;
int a[];
int dp[][];
const int inf=0x3f3f3f3f;
int dfs(int l,int r)
{
if(r-l<) return ;
if(r-l==) return dp[l][r]=a[l]*a[l+]*a[r]; //起始值
if (dp[l][r]!=inf) return dp[l][r];
for(int i=l+;i<r;i++)
dp[l][r]=min(dp[l][r],dfs(l,i)+dfs(i,r)+a[l]*a[i]*a[r]);
return dp[l][r];
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
ans=;
memset(dp,inf,sizeof(dp));
printf("%d\n",dfs(,n));
}
return ;
}

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)

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

  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. PHP中的错误信息

    PHP中的错误信息   php.ini中配置错误消息 在PHP4中,没有异常 Exception这个概念,只有 错误Error.我们可以通过修改php.ini 文件来配置用户端输出的错误信息. 在ph ...

  2. 20145313 《Java程序设计》第十周学习总结

    网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设 ...

  3. creator cocos2d-js-min.js 文件廋身 变小 太大解决方法

    使用的 cocos creator 1.2 版本, 菜单栏 项目 -- 项目设置 -- 模块设置 里面 把不要的模块去掉

  4. JAVA基础补漏--链表

    查询慢:链表中数据不是连续的,每次查询元素,都得从头开始 增删快:链表结构,增加或删除一个数据,对链表的整体结构没有影响,所以快.

  5. 斜率优化dp学习

    用了一堂半的课才彻底搞懂.其他神犇写的博客或多或少有点小bug,所以orzzz不才斗胆重新写一个. 里面大量穿用其他神犇的原话,就不逐一标明出处了. 引用资料 Accept的博客 MathonL的博客 ...

  6. oracle添加用户并给予管理员身份登陆

    sqlplus / as sysdba;--超级管理员sys登陆 show user;--显示当前用户 conn system/admin;--切换用户 CREATE user admin ident ...

  7. [转载]查看JDK及Java框架的源代码

    1.点 "window"-> "Preferences" -> "Java" -> "Installed JRE ...

  8. 你可能不知道的mouseover/mouseout mouseenter/mouseleave

    mouseover与mouseenter 1. 触发时机 mouseover在被监听的节点与子节点上都会触发 mouseenter只在被监听的节点上触发 本质上是因为mouseenter不能冒泡 2. ...

  9. 上传网站后建议执行:chown www:www -R /path/to/dir 对网站目录进行权限设置,/path/to/dir替换为你网站目录。

    上传网站后建议执行:chown www:www -R /path/to/dir 对网站目录进行权限设置,/path/to/dir替换为你网站目录.

  10. JavaScript权威指南--客户端存储

    客户端存储web应用允许使用浏览器提供的API实现将数据存储在用户电脑上. 客户端存储遵循“同源策略”,因此不同站点的页面是无法读取对于存储的数据.而同一站点的不同的页面之间是可以互相共享存储数据的. ...