博客
关于我
poj1651——最优矩阵链乘
阅读量:651 次
发布时间:2019-03-15

本文共 2008 字,大约阅读时间需要 6 分钟。

题目链接:

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

610 1 50 50 20 5

Sample Output

3650

 

题目翻译:

‎乘法谜题使用一行牌进行,每张卡片包含一个正整数。在移动过程中,玩家从行中取出一张牌,并将积分数与所采集卡上的数字和左侧和右侧牌上的数字的分数相等。不允许拿出行中的第一张和最后一张牌。最后移动后,该行中仅剩下两张牌。‎

‎目标是以尽可能减少得分总数的顺序来获取‎
‎牌。‎
‎例如,如果行中的牌包含数字 10 1 50 20 5,则玩家可能会拿一张卡片,其中 1,然后是 20 和 50,‎
‎得分‎

‎10*1*50 * 50*20*5 * 10*50*5 * 500*500*500*2500 * 8000‎

‎如果他以相反的顺序,即50,然后20,然后1,分数将是‎

‎1*50*20 * 1*20*5 * 10*1*5 * 1000*100*50 * 1150。‎

‎输入‎

‎输入的第一行包含卡数 N (3 <= N <= 100)。第二行包含 1 到 100 范围内的 N 个整数,由空格分隔。‎

‎输出‎

‎输出必须包含单个整数 - 最小分数。‎

 

经典的区间dp题,在刘汝佳的紫书(P277)里面也有关于这部分的解析,就不多讲了,具体分析请参考书中的内容。

这个题有两种方法,递推和记忆化搜索,注意递推是要注意 i 和 j 在for循环中的顺序,是按照j-i递增的顺序递推的。

下面给出记忆化搜索的代码和解析

状态:dp[i][j]表示从i区间到j区间的最小分数

转移方程:dp[i][j]=min{dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]}

边界条件:dp[i][i+1]=0

#include 
#include
#include
using namespace std;const int INF=0x3f3f3f3f;int n,a[105],dp[105][105];int solve(int i,int j){ if(dp[i][j]!=INF) return dp[i][j]; if(j==i+1) return 0; for(int k=i+1;k

 

转载地址:http://xifmz.baihongyu.com/

你可能感兴趣的文章
mysql创建函数报错_mysql在创建存储函数时报错
查看>>
mysql创建数据库和用户 并授权
查看>>
mysql创建数据库指定字符集
查看>>
MySql创建数据表
查看>>
MySQL创建新用户以及ERROR 1396 (HY000)问题解决
查看>>
MySQL创建用户与授权
查看>>
MySQL创建用户报错:ERROR 1396 (HY000): Operation CREATE USER failed for 'slave'@'%'
查看>>
MySQL创建索引时提示“Specified key was too long; max key length is 767 bytes”
查看>>
mysql初始密码错误问题
查看>>
MySQL删除数据几种情况以及是否释放磁盘空间【转】
查看>>
Mysql删除重复数据通用SQL
查看>>
mysql判断某一张表是否存在的sql语句以及方法
查看>>
mysql加入安装策略_一键安装mysql5.7及密码策略修改方法
查看>>
mysql加强(1)~用户权限介绍、分别使用客户端工具和命令来创建用户和分配权限
查看>>
mysql加强(2)~单表查询、mysql查询常用的函数
查看>>
mysql加强(3)~分组(统计)查询
查看>>
mysql加强(4)~多表查询:笛卡尔积、消除笛卡尔积操作(等值、非等值连接),内连接(隐式连接、显示连接)、外连接、自连接
查看>>
mysql加强(5)~DML 增删改操作和 DQL 查询操作
查看>>
mysql加强(6)~子查询简单介绍、子查询分类
查看>>
mysql加强(7)~事务、事务并发、解决事务并发的方法
查看>>