Multiplication Puzzle ZOJ - 1602

传送门

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

Process to the end of file.

Output

Output file must contain a single integer - the minimal score.


Sample Input

6
10 1 50 50 20 5

Sample Output

3650

题意:一排牌/卡片(一串数字),每次从这些牌中拿走一张牌(首尾两张不能拿),把前一张,这一张,后一张牌上的数字相乘的结果累加,直到只剩下两张牌为止。问所能得到的最小结果是多少。

    例如:5张牌是10,1,50,20,5。拿走的牌的顺序如果是50,20,1。得到的结果就是:

    1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150;

题解:区间DP

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e2+;
const int mod = 1e9+;
int a[maxn];
int dp[maxn][maxn];
int main()
{ int n;
while(~scanf("%d",&n)) {
memset(dp,INF,sizeof dp);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
for (int i = ; i <= n; i++)
dp[i][i] = dp[i - ][i] = dp[i][i + ] = ;
for (int i = ; i <= n - ; i++)
dp[i - ][i + ] = a[i] * a[i - ] * a[i + ];
for (int len = ; len <= n - ; len++)
for (int i = ; i + len <= n; i++) {
int j = i + len;
for (int k = i + ; k < j; k++)
dp[i][j] = min(dp[i][j], dp[i][k] + a[i] * a[k] * a[j] + dp[k][j]);
}
printf("%d\n", dp[][n]);
}
}

Multiplication Puzzle ZOJ - 1602的更多相关文章

  1. ZOJ 1602 Multiplication Puzzle(区间DP)题解

    题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...

  2. poj 1651 Multiplication Puzzle (区间dp)

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

  3. POJ1651: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)

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

  5. Poj 1651 Multiplication Puzzle(区间dp)

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

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

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

  7. POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP

    题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65 ...

  8. xtu read problem training 4 B - Multiplication Puzzle

    Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...

  9. 题解【POJ1651】Multiplication Puzzle

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

随机推荐

  1. kafka的分区分配策略

    用过 Kafka 的同学应该都知道,每个 Topic 一般会有很多个 partitions.为了使得我们能够及时消费消息,我们也可能会启动多个 Consumer 去消费,而每个 Consumer 又会 ...

  2. 【菜鸟学Linux】Cron Job定期删除Log(日志)文件

    以前一直做Windows开发,近期的项目中要求使用Linux.作为小菜鸟一枚,赶紧买了一本经典书<鸟哥的Linux私房菜>学习.最近刚好有一个小任务 - 由于产品产生的Log很多,而且增长 ...

  3. TP5.1:实现分页

    前提: (1)为了让分页变得更加好看,我的案例加载了bootstrap和jq的文件,具体操作请参考:http://www.cnblogs.com/finalanddistance/p/9033916. ...

  4. 2018.9.8pat秋季甲级考试

    第一次参加pat考试,结果很惨,只做了中间两道题,还有一个测试点错误,所以最终只得了不到50分.题目是甲级练习题的1148-1151. 考试时有些紧张,第一题第二题开始测试样例都运行不正确,但是调试程 ...

  5. MySQL入门很简单: 2 MySQL数据类型

    2. MySQL数据类型 2.1 整数类型 后面的是默认显示宽度: tinyint(4) smallint(6) mediumint(9) int(11) bigint(20) 2.2 浮点型和定点数 ...

  6. selenium入门14 窗口切换

    窗口切换: 当前窗口句柄 current_window_handle 所有的窗口句柄 window_handles 切换窗口 switch_to_window() #coding=utf-8 #切换窗 ...

  7. Maven安装以及使用

    maven安装 1.解压maven安装包 2.右键“计算机”,选择“属性”,之后点击“高级系统设置”,点击“环境变量”,来设置环境变量. 3. 新建系统变量 MAVEN_HOME 变量值:C:\apa ...

  8. segment and section for c++ elf

    http://blog.csdn.net/jiafu1115/article/details/12992497 写一个汇编程序保存成文本文件max.s. 汇编器读取这个文本文件转换成目标文件max.o ...

  9. create-react-app项目使用假数据

    做新项目的时候,前端每次要等后端接口准备好再开始,就会延期,等后端接口准备好了,前端这边的项目又会相互紧张,如果前端跟后端同时进行,前期将框架,基础做好,定好接口文档,前端在后端没准备好接口的时候使用 ...

  10. CentOS 6\7修改主机名

    1.CentOS6修改主机名 1)临时修改主机名: 显示主机名: oracle@localhost:~$ hostname localhost 修改 oracle@localhost:~$ sudo ...