UVA 10304 Optimal Binary Search Tree
简单区间DP。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; const int maxn = ;
int n;
int a[maxn], sum[maxn], dp[maxn][maxn]; int main()
{
while (~scanf("%d", &n))
{
sum[] = ;
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= n; i++) sum[i] = sum[i - ] + a[i]; memset(dp, , sizeof dp); for (int i = ; i <= n; i++)
{
for (int j = ; j+i- <= n; j++)
{
int st = j, en = j + i - ;
int ans = 0x7FFFFFFF;
for (int k = st; k <= en; k++)
ans = min(dp[st][k - ] + dp[k + ][en] + sum[en] - sum[st - ] - a[k],ans);
dp[st][en] = ans;
}
}
printf("%d\n", dp[][n]);
}
return ;
}
UVA 10304 Optimal Binary Search Tree的更多相关文章
- uva 10304 - Optimal Binary Search Tree 区间dp
题目链接 给n个数, 这n个数的值是从小到大的, 给出个n个数的出现次数. 然后用他们组成一个bst.访问每一个数的代价是这个点的深度*这个点访问的次数. 问你代价最小值是多少. 区间dp的时候, 如 ...
- ITA 15.5 Optimal binary search trees
p400 页最后一段 When j >= i , we need to select a root kr from among ki ... kj and then make an optima ...
- Optimal binary search trees
问题 该问题的实际应用 Suppose that we are designing a program to translate text from English to French. For ea ...
- UVA 1264 - Binary Search Tree(BST+计数)
UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
随机推荐
- win7系统,apache2.2下添加PHP5的配置详解
首先要说apache(服务器). php(开发语言). mysql(数据库) 之间的关系. Apache:为系统提供了Web服务支持,网站:http://www.apache.org/ PHP:为系统 ...
- mysql命令行操作 添加字段,修改字段
alter table t_apply change column remarks(原) apply_remarks(目标) varchar(100) default '' c ...
- Hadoop概论
1.Hadoop核心项目:HDFS(分布式文件系统)和MapReduce(并行计算框架) 2.HDFS的架构 主从结构 主节点,只有一个:namenode(接受用户操作要求:维护文件系统的目录结构:管 ...
- .NET面试题目二
1.对你来说工作中最重要的是什么? 答:团队目标的实现.(这是所有公司希望员工拥有的素质) 2.为什么愿意为本公司工作? 答:因为我认为我可以与贵公司取得共同的发展.(暗示留下我可能为公司带来的益处) ...
- spring security 3 自定义认证,授权示例
1,建一个web project,并导入所有需要的lib. 2,配置web.xml,使用Spring的机制装载: <?xml version="1.0" encoding=& ...
- android view构造函数研究
上周遇到了SurfaceView的constructor的问题,周末决定略微细致地研究一下这个令人发指的玩意. SurfaceView是View的子类,与View一样有三个const ...
- git 创建分支,删除分支,管理分支
参考 http://blog.csdn.net/dijason/article/details/9042425 查看分支: 1 查看本地分支: $ git branch 2 查看远程分支 $ g ...
- 详细的SQL中datediff用法
DATEDIFF 函数 [日期和时间] 功能返回两个日期之间的间隔. 语法DATEDIFF ( date-part, date-expression-1, date-expression-2 ) da ...
- iOS 10推送通知开发
原文地址:Developing Push Notifications for iOS 10,译者:李剑飞 虽然通知经常被过度使用,但是通知确实是一种获得用户关注和通知他们需要更新或行动的有效方式.iO ...
- bjective-C 中核心处理字符串的类是 NSString 与 NSMutableString
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...