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 ...
随机推荐
- poi操作excel设置数据有效性
private void setDataValidationList(short firstRow,short endRow,short firstCol, short endCol,String d ...
- 序列化layer创建的弹出表单并ajax提交
/** *createTime:2015-09-13 *updateTime:2015-09-13 *author:刘俊 *phone:13469119119 *QQ:418873053 **/ va ...
- HTML+CSS学习
1.彻底弄懂CSS盒子模式(DIV布局快速入门) 2.在CSS中,BOX的Padding属性的数值赋予顺序为padding:10px; 四个内边距都是10px padding:5px 10px; 上下 ...
- 改进我们的小游戏 - 零基础入门学习Python004
改进我们的小游戏 让编程改变世界 Change the world by program 改进我们的小游戏 很多鱼油对改善这个游戏提出了建议,小甲鱼做了一下总结,大概有以下几个方面需要改进: 猜错的时 ...
- Nightmare(BFS)
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...
- nodejs 如何使用upgrade,并且C/B 发送消息
1 const http = require('http'); const querystring = require("querystring"); var postData = ...
- SQL Server 索引的图形界面操作 <第十二篇>
一.索引的图形界面操作 SQL Server非常强大的就是图形界面操作.关于索引方面也一样那么强大,很多操作比如说重建索引啊,查看各种统计信息啊,都能够通过图形界面快速查看和操作,下面来看看SQL S ...
- tar解压错误
# tar -zxvf aaa.tar.gz tar: This does not look like a tar archive tar: Skipping to next header ...
- 【转】 怎么刷入BOOT.IMG(刷机后开机卡在第一屏的童鞋请注意)-------不错不错
原文网址:http://bbs.gfan.com/android-3440837-1-1.html 之前呢,有好多机油问我关于刷机卡屏的问题,我解答了好多,但一一解答太费事了,在这里给大家发个贴吧.其 ...
- 【转】 linux内核移植和驱动添加(三)
原文网址:http://blog.chinaunix.net/uid-29589379-id-4708909.html 原文地址:linux内核移植和驱动添加(三) 作者:genehang 四,LED ...