uva 10304 - Optimal Binary Search Tree 区间dp
给n个数, 这n个数的值是从小到大的, 给出个n个数的出现次数。 然后用他们组成一个bst。访问每一个数的代价是这个点的深度*这个点访问的次数。 问你代价最小值是多少。
区间dp的时候, 如果l >= r, 那么返回0, l == r-1, 返回两个数中小的一个。 其他情况的话枚举分界点进行状态转移。
#include <bits/stdc++.h>
using namespace std;
#define mem1(a) memset(a, -1, sizeof(a))
const int inf = ;
int dp[][], a[], pre[];
int dfs(int l, int r)
{
if(~dp[l][r])
return dp[l][r];
if(l >= r)
return dp[l][r] = ;
if(l == r - ) {
return dp[l][r] = min(a[l], a[r]);
}
dp[l][r] = inf;
for(int i = l-; i < r; i++) {
dp[l][r] = min(dp[l][r], dfs(l, i) + dfs(i + , r) + pre[r]-pre[l-] - a[i+]);
}
return dp[l][r];
}
int main()
{
int n; while(~scanf("%d", &n)) {
mem1(dp);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
pre[i] = pre[i-] + a[i];
}
printf("%d\n", dfs(, n));
}
return ;
}
uva 10304 - Optimal Binary Search Tree 区间dp的更多相关文章
- UVA 10304 Optimal Binary Search Tree
简单区间DP. #include<cstdio> #include<cstring> #include<cmath> #include<vector> ...
- 【XSY2332】Randomized Binary Search Tree 概率DP FFT
题目描述 \(\forall 0\leq i<n\),求有多少棵\(n\)个点,权值和优先级完全随机的treap的树高为\(i\). \(n\leq 30000\) 题解 设\(f_{i,j}\ ...
- 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] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- LeetCode 669 Trim a Binary Search Tree 解题报告
题目要求 Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so t ...
- LeetCode: Validate Binary Search Tree [098]
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
随机推荐
- ZendFramework2 源码分析 init_autoloader.php
// Composer autoloading if (file_exists('vendor/autoload.php')) { // 加载自动加载器 $loader = include 'vend ...
- 在Python3.5下安装和测试Scrapy爬网站
1. 引言 Scrapy框架结构清晰,基于twisted的异步架构可以充分利用计算机资源,是爬虫做大的必备基础.本文将讲解如何快速安装此框架并使用起来. 2. 安装Twisted 2.1 同安装Lxm ...
- wordpress教程之函数讲解
wordpress函数收集 is_home() : 是否为主页is_single() : 是否为内容页(Post), 是否是单篇文章 is_page() : 是否为内容页(Page), 是否 ...
- SPI总线的4种工作模式
spi总线的4种工作模式 0 to 4 modes SPI接口的全称是"Serial Peripheral Interface",意为串行外围接口,是Motorola首先在其MC6 ...
- Redis应用场景-整理
1. MySql+Memcached架构的问题 Memcached采用客户端-服务器的架构,客户端和服务器端的通讯使用自定义的协议标准,只要满足协议格式要求,客户端Library可以用任何语言实现. ...
- 学习 ExtJS 4 面板与布局
原文 http://www.cnblogs.com/codealone/archive/2013/06/04/3091325.html 面板Panel Ext.panel.Panel拓展自Ext.co ...
- Lisp之根源
原文:http://www.paulgraham.com/rootsoflisp.html 约翰麦卡锡于1960年发表了一篇非凡的论文,他在这篇论文中对编程的贡献有如 欧几里德对几何的贡献.1 他向我 ...
- UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>
H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Others) Subm ...
- python中的那些“神器”
"武林至尊,宝刀屠龙,号令天下,莫敢不从,倚天不出,谁与争锋",这是神器.不过今天要说的python中的"神器"就没有这么厉害了,这里要说的"神器&q ...
- 徐汉彬:亿级Web系统搭建—单机到分布式集群
当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...