题目链接:https://vjudge.net/problem/POJ-1651

Multiplication Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11239   Accepted: 6980

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

题意:

有N张写有数字的卡片排成一行,按一定次序从中拿走N-2张(第1张和最后一张不能拿),每次只拿一张,取走一张卡片的同时,会得到一个分数,分值的计算方法是:要拿的卡片,和它左右两边的卡片,这三张卡片上数字的乘积。按不同的顺序取走N-2张卡片,得到的总分可能不相同,求出给定一组卡片按上述规则拿取的最小得分。

题解:

最优矩阵链乘问题。刘汝佳紫书P277。

1.在取牌的过程中,必定存在一张最后取的牌,可知这张牌左右两边的牌都已经取完了(除了两端点)。于是“最后取的牌”,就把区间分成了两段。由于这两段区间的取牌(即得分情况)互不影响,故可以分开求值。那又怎样才能知道最后取哪张牌才能使得取值最优呢?枚举区间内每一张牌作为最后取的牌,取最优值。

2.对于被分开的左右两段区间,又可各自根据步骤1得到最优值。

3.紫书中区间的写法是左闭右开,但个人觉得不好理解,于是就写成自己所熟悉的闭区间。

记忆化搜索:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; LL p[maxn], dp[maxn][maxn]; LL dfs(int l, int r) //区间[l, r]中每个元素都可以取走
{
if(l>r) return ;
if(dp[l][r]!=LNF) return dp[l][r]; for(int k = l; k<=r; k++)
dp[l][r] = min(dp[l][r], dfs(l,k-) + dfs(k+,r) + p[l-]*p[k]*p[r+]); return dp[l][r];
} int main()
{
int n;
scanf("%d",&n);
for(int i = ; i<=n; i++)
scanf("%lld",&p[i]); for(int i = ; i<n; i++)
for(int j = ; j<=n; j++)
dp[i][j] = LNF; cout<<dfs(, n-)<<endl;
return ;
}

递推:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; LL p[maxn], dp[maxn][maxn]; int main()
{
int n;
scanf("%d",&n);
for(int i = ; i<=n; i++)
scanf("%lld",&p[i]); memset(dp, , sizeof(dp));
for(int l = ; l<=n-; l++)
for(int r = l; r<=n-; r++)
dp[l][r] = LNF; for(int len = ; len<=n-; len++)
{
for(int l = ; l<=n--len+; l++) //能取的区间为[2, n-1]
{
int r = l + len - ;
for(int k = l; k<=r; k++)
dp[l][r] = min(dp[l][r], dp[l][k-] + dp[k+][r] + p[l-]*p[k]*p[r+]);
}
} cout<< dp[][n-] <<endl; }

POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP的更多相关文章

  1. UVa 10003 切木棍(区间DP+最优矩阵链乘)

    https://vjudge.net/problem/UVA-10003 题意: 有一根长度为L的棍子,还有n个切割点的位置.你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每 ...

  2. ZOJ 1276 "Optimal Array Multiplication Sequence"(最优矩阵链乘问题+区间DP)

    传送门 •题意 矩阵 A(n×m) 和矩阵 B(m×k) 相乘,共做 n×m×k 次乘法运算: 给你 n 个矩阵,求这 n 个矩阵的最优结合方式,使得做的总乘法运算次数最少: •题解 定义dp(i,j ...

  3. POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)

    传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K T ...

  4. POJ1651:Multiplication Puzzle(区间DP)

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

  5. POJ1651:Multiplication Puzzle(区间dp)

    Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9419 Accepted: 5850 ...

  6. poj1651 Multiplication Puzzle

    比较特别的区间dp.小的区间转移大的区间时,也要枚举断点.不过和普通的区间dp比,断点有特殊意义.表示断点是区间最后取走的点.而且一个区间表示两端都不取走时中间取走的最小花费. #include &l ...

  7. dp方法论——由矩阵相乘问题学习dp解题思路

    前篇戳:dp入门——由分杆问题认识动态规划 导语 刷过一些算法题,就会十分珍惜“方法论”这种东西.Leetcode上只有题目.讨论和答案,没有方法论.往往答案看起来十分切中要害,但是从看题目到得到思路 ...

  8. 蓝桥 ADV-232 算法提高 矩阵乘法 【区间DP】

      算法提高 矩阵乘法   时间限制:3.0s   内存限制:256.0MB      问题描述 有n个矩阵,大小分别为a0*a1, a1*a2, a2*a3, ..., a[n-1]*a[n],现要 ...

  9. tyvj 1198 矩阵连乘——区间dp

    tyvj 1198 矩阵连乘 题目描述 一个n*m矩阵由n行m列共n*m个数排列而成.两个矩阵A和B可以相乘当且仅当A的列数等于B的行数.一个N*M的矩阵乘以一个M*P的矩阵等于一个N*P的矩阵,运算 ...

随机推荐

  1. hihocoder #1290 : Demo Day

    传送门 #1290 : Demo Day 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics star ...

  2. 自定义Navigation按钮及Title

    导航栏自带的按钮,时常不能满足要求,所以深深需要进行各种定制. 写一个UINavigationItem的category // UINavigationItem+CB_ChangeButton.h 
 ...

  3. win10+Linux18.04双系统安装

    给好多可爱的妹子重装了那么多次电脑,懒得码过程,因为我一般每次都要查一查...这次来个综合版吧,超简单,无脑操作. 首先说一下我的电脑Thinkpad + 500G 硬盘 (2014年买的老电脑) 首 ...

  4. 2018 ICPC 沈阳网络赛预赛 Supreme Number(找规律)

    [传送门]https://nanti.jisuanke.com/t/31452 [题目大意]:给定一个数字(最大可达10100),现在要求不超过它的最大超级质数.超级质数定义:对于一个数,把它看成数字 ...

  5. BZOJ1017魔兽地图DotR 樹形DP

    @(BZOJ)[樹形DP, 三維DP] Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA ...

  6. ByteArrayInputStream的作用,和BufferedOutputStream 的区别

    个人好奇ByteArrayInputStream,到底是有什么用于是百度了一些资料 整合了下,********这两个类对于要创建临时性文件的程序以及网络数据的传输.数据压缩后的传输等可以提高运行的的效 ...

  7. Mysql 性能优化20个原则(2)

    5. 在Join表的时候使用相当类型的例,并将其索引 如果你的应用程序有很多 JOIN 查询,你应该确认两个表中Join的字段是被建过索引的.这样,MySQL内部会启动为你优化Join的SQL语句的机 ...

  8. Python爬虫之简单的图片获取

    简单的静态网页的图片获取: import urllib import re import os url = 'http://www.toutiao.com/a6467889113046450702/' ...

  9. 数据库官方在线文档列表(mysql, postgreSQL)

    1. mysql http://dev.mysql.com/doc/ 2. postgreSQL https://www.postgresql.org/docs/

  10. U盘容纳不了大于4G的文件比如ISO文件咋办?

    格式化U盘成NTFS格式就行了,不这么做8,16,32G Upan都容纳不下来.