POJ 1651 Multiplication Puzzle (区间DP)
Description
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
Output
Sample Input
6
10 1 50 50 20 5
Sample Output
3650
题解: 设dp[i][[j] 为从i到j取完出了a[i]和a[j]这两个数的最小代价。
终于所要求出的是dp[1][n]。
如果在i到j区间内最后一个取a[k],那么子问题便能够看得出来:求出dp[i][k]和dp[k][j]的最小代价。求出这两个子问题的代价再加上最后一个取出a[k]的代价,即为dp[i][j]的代价。
问题转化为求出子问题的代价,直到仅仅剩下三个数为止,三个数仅仅能取中间的数。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define lson o << 1, l, m
#define rson o << 1|1, m+1, r
using namespace std;
typedef long long LL;
const int MAX=0x3f3f3f3f;
const int maxn = 100+10;
int n, dp[maxn][maxn], a[maxn];
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
for(int i = 2; i <= n-1; i++) dp[i-1][i+1] = a[i-1]*a[i]*a[i+1]; //边界,三个数仅仅能取中间的数
for(int len = 4; len <= n; len ++)
for(int i = 1; i <= n-len+1; i++) {
int j = i+len-1;
dp[i][j] = MAX;
for(int k = i+1; k <= j-1; k++)
dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j] + a[j]*a[k]*a[i]);
}
printf("%d\n", dp[1][n]);
return 0;
}
POJ 1651 Multiplication Puzzle (区间DP)的更多相关文章
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- POJ 1651 Multiplication Puzzle 区间dp(水
题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...
- POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)
传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K T ...
- Poj 1651 Multiplication Puzzle(区间dp)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10010 Accepted: ...
- POJ 1651 Multiplication Puzzle (区间DP,经典)
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- poj 1651 Multiplication Puzzle【区间DP】
题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...
- poj 1651 Multiplication Puzzle
题目链接:http://poj.org/problem?id=1651 思路:除了头尾两个数不能取之外,要求把所有的数取完,每取一个数都要花费这个数与相邻两个数乘积的代价,需要这个代价是最小的 用dp ...
- POJ 1651 Mulitiplication Puzzle
The multiplication puzzle is played with a row of cards, each containing a single positive integer. ...
随机推荐
- 手动制作用于启动BeagleBoneBlack(am335x)的SD
1.需求MLO.u-boot.img.uImage.Systemfile 获取MLO.u-boot.img下载U-boot源码,解压获得源码,进入源码目录 cd /home/zyr/Source_co ...
- Win10如何配置Jdk环境变量
对于每一位做Java开发的朋友来说,Jdk是必须要安装的,安装好了Jdk,其实并没有结束,还需要配置Jdk的环境变量,系统在不断地更新,小编给大家介绍一下如何在Win10下配置Jdk,并检测是否配置成 ...
- python装饰器原理
妙处在于装饰器的两个return 1.装饰器 # 使用闭包 def wrap(fun): def check(): print("正在检查用户权限!") fun() return ...
- Beego 和 Bee 的开发实例
Beego不是一般的web开发包.它构建在大量已存在的Go之上,提供了许多的功能,以下是提供的功能: 一个完整的ORM 缓存 支持session 国际化(i18n) 实时监测和重载 发布支持 ==== ...
- windows提权exp列表
漏洞列表 #Security Bulletin #KB #Description #Operating System CVE-2017-0213 [Windows COM Elevation of P ...
- linux中的文件编码及编码修改
查看文件编码 在Linux中查看文件编码可以通过以下几种方式: 1.在Vim中可以直接查看文件编码 :set fileencoding 即可显示文件编码格式. 如果你只是想查看其它编码格式的文件或者想 ...
- Java NIo 笔记001
1. Channel Channel接口只提供了两个方法: package java.nio.channels; public interface Channel { public boolean i ...
- POJ Anniversary party 树形DP
/* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...
- C++反汇编-菱形继承
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记.马上就要出差了,回来后接着写吧. 一.概述 菱形继承是最复杂的对象结构,菱形结构会将单一继承与多重继承进行组合. ...
- 华为S5300系列交换机V200R001SPH027升级补丁
S5300SI-V200R001SPH027.pat 附件: 链接:https://pan.baidu.com/s/1ulE0j5Rp4xMkAaOjNGKLMA 密码:d5ze