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. ...
随机推荐
- mongodb cmd 常用命令
如题,命令如下: 1.连接远程数据库命令 mongo -u admin -p admin 192.168.0.197:27017/test 2.查看当前版本 db.version(); 3.mongo ...
- CSS基础-DAY1
CSS 概述CSS 指层叠样式表 (Cascading Style Sheets),样式定义了如何显示 HTML文件中的标签元素,CSS是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标 ...
- Educational Codeforces Round 45 (Div 2) (A~G)
目录 Codeforces 990 A.Commentary Boxes B.Micro-World C.Bracket Sequences Concatenation Problem D.Graph ...
- 【推导】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) B. Mystical Mosaic
题意:给你一个棋盘的最终局面. 你的一次操作可以选择一些行和列,将它们的交叉点染黑,不能重复选择某行或者某列.问你是否能经过数次操作之后,达到目标局面. 就枚举所有黑点,如果该点行列都没被标记,就给它 ...
- [CodeChef-QTREE6]Query on a tree VI
题目大意: 给你一棵黑白树,每个点默认是白色,要求支持以下两种操作: 1.改变一个点的颜色: 2.除去连接不同颜色的点的边,求某个点连通块的大小. 思路: 对原树维护两个树链剖分, 一棵维护当点x为白 ...
- 【BZOJ】4985: 评分【DP】
4985: 评分 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 148 Solved: 75[Submit][Status][Discuss] Des ...
- 重温PHP面向对象的三大特性
PHP面向对象的三大特性:封装性.继承性.多态性. 1. 封装性: 也称为信息隐藏,就是将一个类的使用和实现分开,只保留部分接口和方法与外部联系,或者说只公开了一些供开发人员使用的方法. 于是开发人员 ...
- USB ISP(ICSP) Open Programmer < PWM ADC HV PID >
http://sourceforge.net/projects/openprogrammer/?source=navbar Open Programmer http://openprog.alterv ...
- Linux- systemd
systemd被设计用来改进sysvinit的缺点,它和ubuntu的upstart是竞争对手,预计会取代它们.systemd的很多概念来源于苹果的launchd.创始人Lennart是redhat员 ...
- [Node.js]NET模块
摘要 net模块提供了一些用于底层的网络通信的小工具,包含了创建服务器和客户端的方法.可以使用该模块模拟请求等操作. net模块 引入net模块 var net=require("net&q ...