博客
关于我
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/

你可能感兴趣的文章
NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
查看>>
NIFI同步MySql数据源数据_到原始库hbase_同时对数据进行实时分析处理_同步到清洗库_实际操作06---大数据之Nifi工作笔记0046
查看>>
Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
查看>>
【Flink】Flink 1.9 版本 web UI 突然没有日志
查看>>
NIFI大数据进阶_FlowFile拓扑_对FlowFile内容和属性的修改删除添加_介绍和描述_以及实际操作---大数据之Nifi工作笔记0023
查看>>
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_处理器介绍_处理过程说明---大数据之Nifi工作笔记0019
查看>>
NIFI大数据进阶_FlowFile生成器_GenerateFlowFile处理器_ReplaceText处理器_实际操作---大数据之Nifi工作笔记0020
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_实际操作_02---大数据之Nifi工作笔记0032
查看>>
NIFI大数据进阶_Json内容转换为Hive支持的文本格式_操作方法说明_01_EvaluteJsonPath处理器---大数据之Nifi工作笔记0031
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka消费者处理器_来消费kafka数据---大数据之Nifi工作笔记0037
查看>>
NIFI大数据进阶_Kafka使用相关说明_实际操作Kafka生产者---大数据之Nifi工作笔记0036
查看>>
NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
查看>>
NIFI大数据进阶_NIFI监控功能实际操作_Summary查看系统和处理器运行情况_viewDataProvenance查看_---大数据之Nifi工作笔记0026
查看>>
NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
查看>>
NIFI大数据进阶_NIFI集群知识点_认识NIFI集群以及集群的组成部分---大数据之Nifi工作笔记0014
查看>>
NIFI大数据进阶_NIFI集群知识点_集群的断开_重连_退役_卸载_总结---大数据之Nifi工作笔记0018
查看>>
NIFI大数据进阶_使用NIFI表达式语言_来获取自定义属性中的数据_NIFI表达式使用体验---大数据之Nifi工作笔记0024
查看>>
NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
查看>>
NIFI大数据进阶_内嵌ZK模式集群2_实际操作搭建NIFI内嵌模式集群---大数据之Nifi工作笔记0016
查看>>
NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
查看>>