POJ 1651 Multiplication Puzzle 区间dp(水
2024-09-08 03:52:42
题目链接: 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;
}
最新文章
- js遍历json
- 使用excel计算指数平滑和移动平均
- Android自动化学习笔记之MonkeyRunner:用Eclipse执行MonkeyRunner脚本
- MVC如何在单独的类库中添加区域
- easyui datagrid 列显示和隐藏
- stuts-security.xml
- Could not allocate CursorWindow size due to error -12 错误解决方法
- hdoj 2045 不容易系列之(3)—— LELE的RPG难题
- UVa11613 Acme Corporation(最小费用流)
- 在Ajax中将数组转换成字符串(0517-am)
- Python基础第四天
- websphere安装
- mongodb的NUMA问题
- ABP 数据迁移
- windows应用程序框架及实例
- iOS安全系列之一:HTTPS
- 提交一个变量或数组到另一个jsp页面
- [Hibernate] 分页查询
- lucene简单搜索demo
- 《JavaScript-The Definitive Guide》读书笔记:函数定义和函数调用
热门文章
- shell 循环 read line
- 下载kaggle数据集,验证手机号
- Android中notifyDataSetInvalidated()和notifyDataSetChanged()有什么区别
- Tab键可访问的下拉菜单demo
- HTML5中手势原理分析与数学知识的实践
- 在 Sublime Text 直接运行 Javascript 调试控制台
- js给<;img>;的src赋值问题
- [luoguP1045] 麦森数(快速幂 + 高精度)
- 整体二分--BZOJ1901: Zju2112 Dynamic Rankings
- redis持久化机制【十三】