POJ 1651 Multiplication Puzzle 区间dp(水
题目链接: id=1651">点击打开链
题意:
给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1]
问最小价值
思路:
dp[l,r] = min( dp[l, i] + dp[i, r] + a[l] * a[i] * a[r]);
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 105;
const int inf = 100000000;
int a[N], dp[N][N], n;
int dfs(int l, int r){//返回 把区间l,r合并,最后仅仅剩下a[l]和a[r]能获得的最小价值
if(l > r) return 0;
if(dp[l][r] != -1)return dp[l][r];//若区间[l,r]已经计算过则不再计算
if(l == r || l+1 == r) return dp[l][r] = 0;//仅仅有1个或者2个元素则价值是0
int ans = inf;
for(int i = l+1; i < r; i++)
ans = min(ans, a[i] * a[l] * a[r] + dfs(l, i) + dfs(i, r)); return dp[l][r] = ans;
}
int main(){
while(cin>>n){
for(int i = 1; i <= n; i++)scanf("%d", &a[i]);
memset(dp, -1, sizeof dp);
cout<<dfs(1, n)<<endl;
}
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)
传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K T ...
- POJ 1651 Multiplication Puzzle (区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- 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. ...
随机推荐
- Win2008 Server MySql安装包详细安装教程
首先去官网下载 下载MySql 下载地址:http://downloads.mysql.com/archives/community/ 我这里选择MSI的32位安装包安装,服务器系统32位的. 安装M ...
- Python之面向对象上下文管理协议
Python之面向对象上下文管理协议 析构函数: import time class Open: def __init__(self,filepath,mode='r',encode='utf-8') ...
- Courses on Turbulence
Courses on Turbulence Table of Contents 1. Lecture 1.1. UIUC Renewable energy and turbulent environm ...
- orcad中的快捷键
在画原理图的时候,不能正常的将将要放下的器件与旁边的对其,一种解决办法是按F6,调出大的水平竖直线,在按F6,此线标消失. Ctrl+F8是全屏模式,关闭的方法暂时不知道,退出方式是点击按钮. F10 ...
- String类的概述和构造方法
StringDemo.java /* * String:字符串类 * 由多个字符组成的一串数据 * 字符串其本质就是一个字符数组 * * 构造方法: * String(String original) ...
- Node.js & Unix/Linux & NVM
Node.js & Unix/Linux & NVM nvm https://github.com/creationix/nvm https://github.com/xyz-data ...
- [luoguP1944] 最长括号匹配_NOI导刊2009提高(1)
传送门 非常傻的DP. f[i]表示末尾是i的最长的字串 #include <cstdio> #include <cstring> #define N 1000001 int ...
- [luoguP2617] Dynamic Ranking(树状数组 套 主席树 + 离散化)
传送门 BZOJ上是权限题,洛谷赞啊. 求区间 K 大数很简单. 但是如果修改某个数的话,那么就得把这个数及后面所建的主席树都更新一遍 nlogn,显然不行. 所以可以在外面套一个树状数组来优化,树状 ...
- [luoguP1439] 排列LCS问题(DP + 树状数组)
传送门 无重复元素的LCS问题 n2 做法不说了. nlogn 做法 —— 因为LCS问题求的是公共子序列,顺序不影响答案,影响答案的只是两个串的元素是否相同,所以可以交换元素位置. 首先简化一下问题 ...
- jsp页面根据json数据动态生成table
根据需求由于不同的表要在同一个jsp展示,点击某个表名便显示某张表内容,对于java后台传给jsp页面的json形式的数据是怎么动态生成table的呢? 找了好久,终于找到某位前辈的答案,在此表示衷心 ...