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. interrupt_control

    中断的概念CPU在处理过程中,经常需要同外部设备进行交互,交互的方式由“轮询方式”“中断方式” 轮询方式: 方式:在同外设进行交互的过程中,CPU每隔一定的时间状态就去查询相关的状态位,所以在交互期间 ...

  2. springBoot application.properties 基础配置

    # 文件编码 banner.charset= UTF-8 # 文件位置 banner.location= classpath:banner.txt # 日志配置 # 日志配置文件的位置. 例如对于Lo ...

  3. 为什么全部width:100%浏览器边缘存在留白?

    一般浏览器都给body加了外边距,margin:0应该能解决你所遇到的问题.但你很可能又会遇到其他奇怪的现象,比如说p的行高,在不同浏览器上显示不一致,最根本的解决方案还是重置浏览器默认样式. 可以使 ...

  4. [BZOJ4881][Lydsy1705月赛]线段游戏

    首先冷静一下看清问题的本质,是将整个数列分成两个递增子序列. 那么由Dilworth定理得,无解当且仅当数列的最长下降子序列的长度>2,先特判掉. 然后就有一些比较厉害的做法:http://ww ...

  5. 7.4 (java学习笔记)网络编程之TCP

    一.TCP 1.1 TCP(Transmission Control Protocol 传输控制协议),是一种面向连接的,安全的传输协议,但效率相比于UDP而言比较低. TCP传输时需要确保先建立连接 ...

  6. POJ 1321 棋盘问题 DFS 期末前水一水就好……

    A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. bash编程之 ~制作Mini Linux系统~

    说明1:在一个Linux宿主机系统上,通过以上步骤,可以制作一个微小的Linux系统(可以放置在U盘中等),然后在其它的主机(虚拟机或者物理机)上运行,以实现最小化定制系统的目的. 说明2:上图中黑色 ...

  8. HTML5学习笔记1

    1.HTML5概述 继html4和xhtml1.0后的超文本标记语言最新版本.最重要的三项技术:html5核心规范(标签元素),CSS3,JavaScript2008年发布,主要为了补全功能.特点:1 ...

  9. 虚拟信用卡 全球付, 工商银行国际E卡, Bancore, Entropay, Payoneer

    虚拟信用卡 海外网购.购买国外域名空间.ebay等一些国外网站账号的激活这些情况都需要用到国际信用卡, 如果没有信用卡或者有信用卡但是对于安全性有所顾虑怎么办? 其实有一种东西叫做虚拟信用卡,正规银行 ...

  10. Xilinx Platform Usb Cable

    Key Features High-performance FPGA configuration and PROM/CPLD programming Includes innovative FPGA- ...