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<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
const int INF = 0x3f3f3f3f;
int a[N], dp[N][N]; void Work(int n){
memset(dp, , sizeof(dp));
for(int len = ; len < n; len++){
for(int i = ; i <= n-; i++){
int j = i + len;
dp[i][j] = INF;
for(int k = i + ; k < j; k++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + a[i]*a[k]*a[j]);
}
}
printf("%d\n", dp[][n]);
}
int main(){
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
Work( n );
}

区间动态规划 矩阵连乘 Medium的更多相关文章

  1. 动态规划&矩阵连乘

    动态规划&矩阵连乘 动态规划的概念 •     与分治方法类似       分-治-合 • 与分治方法不同       子问题之间并非相互独立 •     基本思想        用一个表记录 ...

  2. 【算法•日更•第十期】树型动态规划&区间动态规划:加分二叉树题解

    废话不多说,直接上题: 1580:加分二叉树 时间限制: 1000 ms         内存限制: 524288 KB提交数: 121     通过数: 91 [题目描述] 原题来自:NOIP 20 ...

  3. 【算法•日更•第三十期】区间动态规划:洛谷P4170 [CQOI2007]涂色题解

    废话不多说,直接上题:  P4170 [CQOI2007]涂色 题目描述 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符 ...

  4. Codeforces 719E [斐波那契区间操作][矩阵快速幂][线段树区间更新]

    /* 题意:给定一个长度为n的序列a. 两种操作: 1.给定区间l r 加上某个数x. 2.查询区间l r sigma(fib(ai)) fib代表斐波那契数列. 思路: 1.矩阵操作,由矩阵快速幂求 ...

  5. BZOJ_3270_博物馆_(高斯消元+期望动态规划+矩阵)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3270 \(n\)个房间,刚开始两个人分别在\(a,b\),每分钟在第\(i\)个房间有\(p[ ...

  6. poj 3744 Scout (Another) YYF I - 概率与期望 - 动态规划 - 矩阵快速幂

      (Another) YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...

  7. (基于Java)算法之动态规划——矩阵连乘问题

    动态规划(Dynamic Programming):与分治法类似,其基本思想也是将待求解问题分解成若干个子问题,先求解子问题,然后从这些子问题的解得到原问题的解.与分治法不同的是,适用于动态规划法求解 ...

  8. LG5337/BZOJ5508 「TJOI2019」甲苯先生的字符串 线性动态规划+矩阵加速

    问题描述 LG5337 BZOJ5508 题解 设\(opt_{i,j}(i \in [1,n],j \in [1,26])\)代表区间\([1,i]\),结尾为\(j\)的写法. 设\(exist_ ...

  9. hdu 2604 Queuing(动态规划—>矩阵快速幂,更通用的模版)

    题目 最早不会写,看了网上的分析,然后终于想明白了矩阵是怎么出来的了,哈哈哈哈. 因为边上的项目排列顺序不一样,所以写出来的矩阵形式也可能不一样,但是都是可以的 //愚钝的我不会写这题,然后百度了,照 ...

随机推荐

  1. CF718C Sasha and Array 线段树 + 矩阵乘法

    有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$   直接求不好求,改成矩阵乘法的形式:  $a_{i}=M^x\times ...

  2. UVa 572 Oil Deposits (Floodfill && DFS)

    题意 :输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横竖以及对角方向),就是说它们属于同一个八连块. 分析 :可以考虑种子填充深搜的方法.两重for循 ...

  3. 【gym102394B】Binary Numbers(DP)

    题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...

  4. 阿里知识储备之二——junit学习以及android单元测试

    一,junit框架 http://blog.csdn.net/afeilxc/article/details/6218908 详细见这篇博客 juit目前已经可以和maven项目进行集成和测试,而且貌 ...

  5. 搭建Ambari 2.6.0 tar 解压缩报错

    背景:我们使用的方式不是wget 去下载ambari的源码包,而是在windows 的 firefox 下直接下载,将文件存储到本地. 执行 tar -zxvf HDP-2.6.3.0-centos7 ...

  6. java 强弱软虚 四种引用,以及用到的场景

    1.利用软引用和弱引用解决OOM问题:用一个HashMap来保存图片的路径和相应图片对象关联的软引用之间的映射关系,在内存不足时,JVM会自动回收这些缓存图片对象所占用的空间,从而有效地避免了OOM的 ...

  7. 2018-2019-2 网络对抗技术 20165235 Exp 9 Web安全基础

    实验任务 本实践的目标理解常用网络攻击技术的基本原理,做不少于7个题目,共3.5分.包括(SQL,XSS,CSRF).Webgoat实践下相关实验. 基础问题回答 (1)SQL注入攻击原理,如何防御 ...

  8. zabbix 内网监控云服务器

    今天 搞监控碰到了一个问题就是.内网机器搭建的zabbix服务器去监控云服务器agent的时候,agent 需要写服务端的IP地址. 我的思路是内网服务器映射自己公网IP地址的zabbix的端口100 ...

  9. 安装mysql数据库要注意的

    只安装基本功能即可,以后要的话可以加 需要配置环境变量 最好不要将数据存放在c盘,默认在  C:\ProgramData\MySQL\MySQL Server 5.6  里面存放建的表和存放的数据

  10. Git:目录

    ylbtech-Git:目录 1.返回顶部 1. https://git-scm.com/ 2. 2.返回顶部 1.Easy Git Integration Tools https://marketp ...