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. nowcoder 79F 小H和圣诞树 换根 DP + 根号分治

    设节点个数大于 $\sqrt n$ 的颜色为关键颜色,那么可以证明关键颜色最多有 $\sqrt n$ 个.对于每个关键颜色,暴力预处理出该颜色到查询中另一个颜色的距离和. 对于不是关键颜色的询问,直接 ...

  2. ROI pooling

    R-CNN需要大量的候选框,对每个候选框都提取特征,速度很慢,无法做到实时检测,无法做到端到端.ROI pooling层实现training和testing的显著加速,并提高检测accuracy. R ...

  3. opengl中相关的计算机图形变换矩阵之:齐次坐标 (摘编)

    模型视图变换(几何变换)矩阵: 1. 齐次坐标:两条平行线也可以相交. 在欧几里得空间中,两条平行线是无法相交的,但是在投影空间(Projective Space)这条定理就不再适用了. 比如上图中, ...

  4. 大哥带我们的mysql注入 基于bool的盲注

    盲注 那么我们来了解一点盲注的语法 这里面是语法的介绍 https://blog.csdn.net/alex_seo/article/details/82148955 0X01第一步我们先判断当前数据 ...

  5. bootstrap editable 行内编辑

    除了那些bootstrap/bootstrap table的js , css之外,要额外添加editable的文件: <link href="../assets/css/bootstr ...

  6. 大数据笔记(二十三)——Scala语言基础

    一.Scala简介:一种多范式的编程语言 (*)面向对象 (*)函数式编程:Scala的最大特点 (*)基于JVM 二.Scala的运行环境 (1)命令行:REPL 进入: scala 退出::qui ...

  7. phpstudy composer 安装

    今天突然发现phpstudy 可以安装 composer 一打开php中openssl拓展 坑一  我的phpstudy 是2018最新版本,但是你下载laravel什么之类库会报错,是由于compo ...

  8. leetcode-mid-dynamic programming- Longest Increasing Subsequence-NO

    不会... 参考: 思路类似于coin那个题,for循环中在满足条件时就及时更新当下位置的信息 def lengthOfLIS(nums): """ :type nums ...

  9. 【洛谷T89359 扫雷】

    题目链接 这个题有一个十分简单的做法 我们可以考虑位运算 按位异或(^) 异或,英文为exclusive OR,缩写成xor 异或(xor)是一个数学运算符.它应用于逻辑运算.异或的数学符号为“⊕”, ...

  10. 创建Stream

    1.创建Stream create Stream from Collections; create Stream from values; create Stream from Arrays; cra ...