题解【POJ1651】Multiplication Puzzle
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
\]
If he would take the cards in the opposite order, i.e. \(50\), then \(20\), then \(1\), the score would be
\]
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
Solution
题意简述:给定一个序列,取走一个数的代价是它乘上它相邻的两个数,两头的数不能取,求最小代价。
考虑区间DP。
令\(dp[i][j]\)表示把\(i+1\)~\(j-1\)的数都取走的最小代价。
然后进行区间DP,注意区间的长度的范围是\(3\)~\(n\)。
状态转移方程:
\]
Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int n, m, dp[103][103], a[103];
int main()
{
memset(dp, 0, sizeof(dp));//初始化
n = gi();
for (int i = 1; i <= n; i++) a[i] = gi();
for (int i = 3; i <= n; i++)//枚举区间长度
{
for (int j = 1; j + i <= n + 1; j++)//枚举起点
{
int k = j + i - 1;//终点
dp[j][k] = 1000000007;//当前区间的dp数组初始化
for (int l = j + 1; l <= k - 1; l++)//枚举分割点
{
dp[j][k] = min(dp[j][k], dp[j][l] + dp[l][k] + a[j] * a[k] * a[l]);//进行状态转移
}
}
}
printf("%d\n", dp[1][n]);//最后输出答案
return 0;//结束
}
题解【POJ1651】Multiplication Puzzle的更多相关文章
- POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP
题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65 ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- POJ1651:Multiplication Puzzle(区间dp)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9419 Accepted: 5850 ...
- poj1651 Multiplication Puzzle
比较特别的区间dp.小的区间转移大的区间时,也要枚举断点.不过和普通的区间dp比,断点有特殊意义.表示断点是区间最后取走的点.而且一个区间表示两端都不取走时中间取走的最小花费. #include &l ...
- POJ1651 Multiplication Puzzle【区间DP】
LINK 每次删除一个数,代价是左右两边相邻的数的当前数的积 第一个和最后一个数不能删除 问最后只剩下第一个数的最后一个数的最小代价 思路 很简单的DP 正着考虑没有办法确定两边的数 那么就把每个区间 ...
- POJ1651 Multiplication Puzzle(相邻乘积之和最小,区间DP)
http://blog.csdn.net/libin56842/article/details/9747021 http://www.cnblogs.com/devil-91/archive/2012 ...
- poj1651 Multiplication Puzzle(简单区间dp)
题目链接:http://poj.org/problem?id=1651 题意:一系列的数字,除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值, 最后将所有价值加起来,要求最小值. ...
- POJ1651 Multiplication Puzzle (区间DP)
这道题的妙处在于把原问题看成矩阵连乘问题,求这些矩阵相乘的最小乘法次数,比如一个i*k矩阵乘一个k*j的矩阵,他们的乘法次数就是i*k*j (联想矩阵乘法的三层循环),题目说的取走一张牌,类似于矩阵相 ...
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
随机推荐
- BZOJ4710: [Jsoi2011]分特产 组合数学 容斥原理
题意:把M堆特产分给N个同学,要求每个同学至少分到一种特产,共有多少种分法? 把A个球分给B个人的分法种数:(插板法,假设A个球互不相同,依次插入,然后除以全排列去重) C(A,B+A) 把M堆特产分 ...
- CSS一些特殊图形
CSS一些特殊图形 CSS绘制三角形 通过控制元素的border属性可以实现三角形效果; 首先来设置4个边框, 为50px solid [color] color设置成不同的颜色值看一下效果 < ...
- 为什么 RMAN 控制文件自动备份的名称格式没有遵循 %F 规则
在 Oracle 中越是简单的问题,往往越难找到答案,举个例子: 你是否留意观察过在 RMAN 进行备份的时候,自动生成的控制文件名称是否是按照 %F 规则来生成的? 关于控制文件自动备份路径格式,在 ...
- PyQt5+Eric6开发的一个使用菜单栏、工具栏和状态栏的示例
前言 在做一个数据分析的桌面端程序遇到一些问题,这里简单整理下,分享出来供使用者参考. 1.网上查使用PyQt5工具栏的示例,发现很多只是一个简单的退出功能,如果有几个按钮如何处理?如何区分点击的究竟 ...
- 使用yaml格式进行接口测试报错
前言:本人公司使用yaml做接口测试.某日开发写了一个字典嵌套列表,列表里面再嵌套字典的接口. yaml的值应该为下图(注意缩进问题) 加了-代表下面是一个列表 {'uid': '3a61479f ...
- python之pandas简介
一. Pandas简介 1.Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和 ...
- 电脑出现kernelbase.dll错误的两种解决方法
KernelBase.dll是Windows操作系统的重要文件,它为各种应用程序提供服务.如果电脑提示kernelbase.dll错误,这该怎么处理?大家可以用电脑自带的防火墙或者是第三方软件来进行故 ...
- 同步选中所有checkbox
$("input[type=checkbox][tag=ckAll]").change(function () $(this).parent().parent().siblings ...
- 【sql】sql必知必会_01
数据: /* Navicat Premium Data Transfer Source Server : localhost_3306 Source Server Type : MySQL Sourc ...
- win10显示“没有有效的IP地址”
可能你没有新建该宽带连接!!!(本人就是蠢到如此地步了_(:з)∠)_)