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 ...
随机推荐
- 创建ListView的基本步骤
参考<疯狂android讲义>第2.5节P94 1.创建一个或者多个ListView <LinearLayout xmlns:android="http://schemas ...
- CodeForces 25E Test KMP
Description Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tes ...
- 基于C#—WPF的扫雷游戏
自学教材:<C#入门经典(第六版)>,1月28日购入,1月29日到2月9日学习了前十六章,由于有C语言基础,在语法阶段学习起来比较轻松,不过在接触到面向对象的时候遇到了一些困难,对于一些概 ...
- WPF笔记(1.9 样式和控件模板)——Hello,WPF!
原文:WPF笔记(1.9 样式和控件模板)--Hello,WPF! 资源的另一个用途是样式设置: <Window > <Window.Resources> <St ...
- ###Git 基础图解、分支图解、全面教程、常用命令###
一.Git 基础图解 转自:http://www.cnblogs.com/yaozhongxiao/p/3811130.html Git 图解剖析 git中文件内容并没有真正存储在索引(.git/in ...
- C# Switch is Type
常规用法: Type t = sender.GetType(); if (t == typeof(Button)) { var realObj = (Button)sender; // Do Some ...
- canvas.js | CLiPS
canvas.js | CLiPS canvas.js The canvas.js module is a simple and robust JavaScript API for the HTML5 ...
- 不可或缺的企业OA面临问题,以及解决建议 软件定制开发 森普演示平台
---恢复内容开始--- 随着信息时代的来临,企业管理也相应的信息化,各种管理软件相继而出,各行各业的信息化有过成功,也有过失败(注:是以该项目是否达到用户的预期目标而言).据统计在信息化失败的案例中 ...
- jsp用jstl标签比较枚举
日向博客最近在优化,有这一样一个小问题,我希望在下面的消息中心页面,未读的消息链接显示蓝色,已读的消息显示红色: 这就需要用jstl做一个判断. 之前的代码是这种形式: 消息中心:<br> ...
- Multiscale Combinatorial Grouping 学习和理解源代码(一)
目标探测由于所做的最新研究.因此,这一领域的一般阅读文章.发现这篇文章,效果是比较新的比较好.在如此仔细研究.贴纸和共享.下面已经发布若干个连续的,分别对论文和代码进行大致地介绍,最后依据自己的实验对 ...