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 ...
随机推荐
- 【Solr专题之九】SolrJ教程
一.SolrJ基础 1.相关资料 API:http://lucene.apache.org/solr/4_9_0/solr-solrj/ apache_solr_ref_guide_4.9.pdf:C ...
- 单机/伪分布式Hadoop2.4.1安装文档
转载自官方文档,最新版请见:http://hadoop.apache.org/docs/r2.4.1/hadoop-project-dist/hadoop-common/SingleCluster.h ...
- 如何使用Prism框架的EventAggregator在模块间进行通信
目的 本文主要介绍如何使用Prism类库提供的事件机制在松耦合组件之间相互通信,Prism类库的事件机制建立在事件聚合服务之上,允许发布者和订阅者通过事件进行通信,不需要彼此之间引用. 事件聚合 Ev ...
- thinkphp项目目录
# ThinkPHP核心文件介绍 ├─ThinkPHP.php 框架入口文件 ├─Common 框架公共文件 ├─Conf 框架配置文件 ├─Extend ...
- Code First 创建数据库
最近在对以前学的知识做一个总结,EF 这块,Code First 是很重要的一部分,方便快捷创建模型. Code First有两种配置方式: DataAnnatation: [Table(&quo ...
- 如果设置Keil从C代码编译出来的hex文件地址从0x8000开始
和MON51的设置一样,这样作:1.把Startup.a51拷贝到工程目录加入工程,修改125行的 CSEG AT 0 为 CSEG AT 0X8000 ...
- TrimPath - Js模板引擎
当页面中引用template.js文件之后,脚本将创建一个TrimPath对象供你使用. parseDOMTemplate(elementId,optionalDocument) //获得模板字符串代 ...
- 51 EEPROM操作模板
各个型号容量及扇区请查datasheet #include <reg52.h> #include "intrins.h" typedef unsigned char b ...
- Grid++Report支持CS/BS模式的表报插件
Grid++Report 可用于开发桌面C/S报表与WEB报表(B/S报表),C/S报表开发适用于VB.NET.C#.VB.VC.Delphi等.WEB报表开发适用于ASP.ASP.NET.JSP/J ...
- python socket之tcp服务器与客户端demo
python socket之tcp服务器与客户端demo 作者:vpoet mails:vpoet_sir@163.com server: # -*- coding: cp936 -*- ''' 建立 ...