题目链接

给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的更多相关文章

  1. UVA 10304 Optimal Binary Search Tree

    简单区间DP. #include<cstdio> #include<cstring> #include<cmath> #include<vector> ...

  2. 【XSY2332】Randomized Binary Search Tree 概率DP FFT

    题目描述 \(\forall 0\leq i<n\),求有多少棵\(n\)个点,权值和优先级完全随机的treap的树高为\(i\). \(n\leq 30000\) 题解 设\(f_{i,j}\ ...

  3. 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 ...

  4. Optimal binary search trees

    问题 该问题的实际应用 Suppose that we are designing a program to translate text from English to French. For ea ...

  5. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  6. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Linux 下配置 SoftEther Client

    我经常使用的代理有 SSH, GoAgent, FreeGate, VPN, HttpProxy 等等,不过 SoftEther 应该是我用过的最快.最稳定的 VPN 协议. Windows 下配置 ...

  2. HTML5简单入门系列(九)

    前言 上篇本来应该就是最后一篇了,但是楼主总觉得没有写上一个简单应用,不算是完整的学习系列.所以增加一篇关于动画的应用,对一个开源动画的介绍(很基础,非楼主原创). 本篇介绍一个基于Canvas的发光 ...

  3. Python爬虫学习:二、爬虫的初步尝试

    我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...

  4. python连接postgresql数据库

    python可以通过第三方模块连接postgresql. 比较有名的有psycopg2  和python3-postgresql (一)psycopg2 ubuntu下安装 sudo apt-get ...

  5. Octorber 21st

    Octorber 21st Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. 段(SEGMENT)

    伪指令SEGMENT被用来声明一个普通段.一个再定位类型(relocation type)和一个地址分配类型(allocation type)可以按如下形式指明: segment SEGMENT cl ...

  7. Socket 编程示例(二)

    利用晚上这点闲暇时间,写了一个Socket通信的小实例,该实例包含服务器端和客户端.其基本工作流程是:当服务器启动服务以后,客户端进行连接,如果连接成功,则用户可以在发送消息框中输入待发送的消息,然后 ...

  8. codeforce343A

    题目地址:http://codeforces.com/problemset/problem/343/A 比赛的时候就囧了,只推出a<b的时候最少需要b个电阻. 后来看了题解,知道 题意:用最少的 ...

  9. 电机转矩T=9550*P/N推导。

    很奇怪,这个公式怎么来的,原来好多是基础物理的,也许我们初中高中物理书上多有,基础真的是很基础的基础. P=F*V (1)  ,即功率=力*速度 T=F*R (2) ,即力矩=力*作用长度 ,在电机里 ...

  10. POJ 3997 Stock Chase

    Stock Chase Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 455   Accepted: 131 Descrip ...