题目链接:

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;
}

最新文章

  1. js遍历json
  2. 使用excel计算指数平滑和移动平均
  3. Android自动化学习笔记之MonkeyRunner:用Eclipse执行MonkeyRunner脚本
  4. MVC如何在单独的类库中添加区域
  5. easyui datagrid 列显示和隐藏
  6. stuts-security.xml
  7. Could not allocate CursorWindow size due to error -12 错误解决方法
  8. hdoj 2045 不容易系列之(3)—— LELE的RPG难题
  9. UVa11613 Acme Corporation(最小费用流)
  10. 在Ajax中将数组转换成字符串(0517-am)
  11. Python基础第四天
  12. websphere安装
  13. mongodb的NUMA问题
  14. ABP 数据迁移
  15. windows应用程序框架及实例
  16. iOS安全系列之一:HTTPS
  17. 提交一个变量或数组到另一个jsp页面
  18. [Hibernate] 分页查询
  19. lucene简单搜索demo
  20. 《JavaScript-The Definitive Guide》读书笔记:函数定义和函数调用

热门文章

  1. shell 循环 read line
  2. 下载kaggle数据集,验证手机号
  3. Android中notifyDataSetInvalidated()和notifyDataSetChanged()有什么区别
  4. Tab键可访问的下拉菜单demo
  5. HTML5中手势原理分析与数学知识的实践
  6. 在 Sublime Text 直接运行 Javascript 调试控制台
  7. js给&lt;img&gt;的src赋值问题
  8. [luoguP1045] 麦森数(快速幂 + 高精度)
  9. 整体二分--BZOJ1901: Zju2112 Dynamic Rankings
  10. redis持久化机制【十三】