Multiplication Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7118   Accepted: 4385

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

题意就是给了几个数,依次拿走中间的数,每一次拿走第i个数都会有相应的代价value[i-1]*value[i]*value[i+1],问要求的是给定的序列 拿走所有中间的数 代价最少是多少。

就是矩阵相乘式DP。。。把那个数单提出来想象成要把它第一个拿走就OK了啊,怎么自己的脑筋总是奔到死胡同里面呢,气死我了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define INF 0x3f3f3f3f int dp[105][105];
int value[105];
int num; int main()
{
int i,j,k,len;
scanf("%d",&num); for(i=1;i<=num;i++)
{
scanf("%d",&value[i]);
dp[i][i]=0;
} for(i=1;i<=num;i++)
{
dp[i][i+1] = 0;
dp[i][i+2] = value[i]*value[i+1]*value[i+2];
} for(len=3;len<=num;len++)
{
for(i=1,j=len+i;j<=num;i++)
{
j=len+i;
dp[i][j]=INF;
for(k=i+1;k<j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+value[k]*value[i]*value[j]);//想象成是第一个拿走的
}
}
}
cout<<dp[1][num]<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1651:Multiplication Puzzle 矩阵相乘式DP的更多相关文章

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

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

  2. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  3. POJ 1651 Multiplication Puzzle (区间DP)

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

  4. Poj 1651 Multiplication Puzzle(区间dp)

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

  5. poj 1651 Multiplication Puzzle【区间DP】

    题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...

  6. POJ 1651 Multiplication Puzzle (区间DP,经典)

    题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...

  7. POJ 1651 Multiplication Puzzle 区间dp(水

    题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...

  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. PAT (Advanced Level) 1124~1127:1124模拟 1125优先队列 1126欧拉通路 1127中序后序求Z字形层序遍历

    1124 Raffle for Weibo Followers(20 分) 题意:微博抽奖,有M个人,标号为1~M.从第S个人开始,每N个人可以获奖,但是已获奖的人不能重复获奖,需要跳过该人把机会留给 ...

  2. day03-Python运维开发基础-(数据类型强转、运算符、逻辑短路、isinstance)

    1. 强制转换成容器数据类型 # ### 强制类型转换 容器类型数据 (str list tuple set ) var1 = "你好世界" var2 = ["陈博文&q ...

  3. ETC系列产品非接触式读卡器方案:SI522

    随着科技的不断发展,出行上高速这是非常寻常的事.但是在很多节假日高峰时期,在高速路口塞车缴费给很多车主造成很大的烦心.为了解决这一系列的问题,科技发明了ETC这种便捷式缴费技术,让车主们顺畅通过高速路 ...

  4. delphi10.2 命令行编译x64脚本

    Build.bat @echo off @echo delphi x64编译测试 @cd /d %~dp0 @echo 设置Delphi参数信息 @set SourcePath=%~dp0.\src ...

  5. JAVA学习笔记-数组的三种初始化方式

      package Study; public class TestArray02 { public static void main(String[] args){//声明 int[] a; int ...

  6. python假设一段楼梯共 n(n>1)个台阶,小朋友一步最多能上 3 个台阶,那么小朋友上这段楼 梯一共有多少种方法

    我们先把前四节种数算出来(自己想是哪几类,如果你不会算,那就放弃写代码吧,干一些在街上卖肉夹馍的小生意,也挣得不少) 标号 1    2    3     4 种类 1    2    4     7 ...

  7. 吴裕雄--天生自然java开发常用类库学习笔记:观察者设计模式

    import java.util.* ; class House extends Observable{ // 表示房子可以被观察 private float price ;// 价钱 public ...

  8. JuJu团队11月28号工作汇报

    JuJu团队11月28号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 于达 解决了数据接口的bug 生成generator形式, 并用熟悉Julia处理数据的方法 处理数据步 ...

  9. checkbox checked属性值

    记住我1<input type='checkbox' /> 记住我2<input type='checkbox' /> <button onclick='hehe();' ...

  10. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...