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. 论 ArrayList如何实现线程安全

    一:使用synchronized关键字 二:使用Collections.synchronizedList(); 假如你创建的代码如下:List<Map<String,Object>& ...

  2. luoguP4571 [JSOI2009]瓶子和燃料 裴蜀定理

    裴蜀定理的扩展 最后返回的一定是\(k\)个数的\(gcd\) 因此对于每个数暴力分解因子统计即可 #include <map> #include <cstdio> #incl ...

  3. SPOJ8791 DYNALCA LCT

    考虑\(LCT\) 不难发现,我们不需要换根... 对于操作\(1\),\(splay(u)\)然后连虚边即可 对于操作\(3\),我们可以先\(access(u)\),然后再\(access(v)\ ...

  4. [BZOJ4557][JLOI2016]侦察守卫(树形DP)

    首先可以确定是树形DP,但这里存在跨子树的信息传递问题,这里就需要“借”的思想. f[i][j]表示i子树内所有点都被覆盖到,且i以外j层内的点都能被覆盖到 的方案数. g[i][j]表示i子树内离i ...

  5. ARC 101 D - Median of Medians

    题面在这里! 这种题只能二分答案把qwwq,直接做根本做不了啊... 首先你需要知道如何通过 一个区间<=x的数有多少个 来判断x和这个区间中位数的关系. 很显然当数有至少 [L/2]+1 个( ...

  6. 【Codeforces 949D】Shake It! 【动态规划】

    参考: http://blog.csdn.net/gjghfd/article/details/77824901 所求的是满足条件的图中“不同构”的数量,意味着操作的顺序是可以忽略的.考虑若干次操作后 ...

  7. Window 下安装

    Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择, ...

  8. MyBatis连接SQL Server的关键点

    一.Maven包配置 <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> < ...

  9. 利用Jenkins实现JavaWeb项目的自动化部署

    修改代码,打包,上传,重启... 大把的时间花费在这些重复无味的工作上.笔者与当前主流的价值观保持一致:我们应该把时间花费在更有意义的事情上.我们可以尝试借助一些工具,让这些重复机械的工作交给计算机去 ...

  10. Mysql 5.6 慢日志配制

    一.配制my.cnf(/etc/my.cnf) slow_query_log=onlong_query_time=1slow_query_log_file=/var/log/slow_query.lo ...