算法:uva-10304 Optimal Binary Search Tree(区间dp)

题意

给一个序列即可 S = (e1,e2,...,en),且e1<e2<..<en.要把这些序列构成一个二叉搜索 树。

二叉搜索树是具有递归性质的,且若它的左子树不空,则左子树上所有结点的值均小于它的根结点的 值; 若它

的右子树不空,则右子树上所有结点的值均大于它的根结点的值。

因为在实际应用中,被访 问频率越高的元素,就应该越接近根节点,这样才能更加节省查找时间。

每个元素有一个访问频率f(ei) ,当元素位于深度为k的地方,那么花费cost(ei) = k.

所有节点的花费和访问频率乘积之和为:

sum = f(e1)*cost(e1) + f(e2)*cost(e2) + ... + f(en)*cost(en)

我们叫sum值最小的二叉搜索树为最优二叉 搜索树。

按顺序给出集合序列S,和每个元素的频率f(ei),求sum的最小值
思路

因为他题目给的 序列是从小到大的,那么对于这个序列的任意一个ei,设ei为根节点,

我们可以知道在序列中ei左边的所 有数会构成ei的左子树,ei的右边的所有数会构成

ei的右子树。

那么我们就可以枚举根节点,然后选 择值最小的一种方案。

说到这里,再结合题目的数据范围,那么很容易可以想到就是区间dp了!

设f(i, j)表示序列区间(i, j)的数构成的一棵最优二叉查找树的值,

当枚举根节点ek时,它的左子树 (wi,wi+1,..,wk-1)的所有节点的深度都会增加1,

那么左子树增加sum(w1,w2,...wk-1)

右子树(ek+1, ek+2,..ej)的值也会增加sum(ek+1,ek+2,...,ej).

可以看出,那么总共会增加sum(i, j) - wk

那 么就可以推出状态转移了:

f(i, j) = min{ f(i,k-1)+f(k+1,j)+sum(i, j) - wk | i<=k<=j}

代码

/**=====================================================
* This is a solution for ACM/ICPC problem
*
* @source : uva-10304 Optimal Binary Search Tree
* @description : 区间dp
* @author : shuangde
* @blog : blog.csdn.net/shuangde800
* @email : zengshuangde@gmail.com
* Copyright (C) 2013/09/06 16:37 All rights reserved.
*======================================================*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;

typedef long long int64;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;

const int MAXN = 210;
int n;
int w[MAXN];
int sum[MAXN];
int f[MAXN][MAXN];

int main(){

while (~scanf("%d", &n)) {

sum[0] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &w[i]);
sum[i] = sum[i-1] + w[i];
}

memset(f, 0, sizeof(f));

for (int d = 2; d <= n; ++d) {
for (int l = 1; l + d - 1 <= n; ++l) {
int r = l + d - 1;
int ans = INF, tot = sum[r] - sum[l-1];
for (int k = l; k <= r; ++k)
ans = min(ans, f[l][k-1] + f[k+1][r] + tot - w[k]);
f[l][r] = ans;
}
}
printf("%d\n", f[1][n]);
}
return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索算法
, search
, 序列
, binary
, 区间
一个
binary search、binary search tree、binarysearch用法、arrays.binarysearch、java binarysearch,以便于您获取更多的相关知识。

时间: 2024-11-03 08:16:41

算法:uva-10304 Optimal Binary Search Tree(区间dp)的相关文章

Geeks 面试题之Optimal Binary Search Tree

Dynamic Programming | Set 24 (Optimal Binary Search Tree) Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys

算法题:UVA 348 Optimal Array Multiplication Sequence(区间dp)

Optimal Array Multiplication Sequence Given two arrays A and B, we can determine the array C = A B using the standard definition of matrix multiplication: The number of columns in the A array must be the same as the number of rows in the B array. Not

UVa 348 Optimal Array Multiplication Sequence:区间DP&amp;amp;矩阵链乘,MCM

348 - Optimal Array Multiplication Sequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=284 记忆化搜索:dp[a][b] = max(dp[a][b], dp[a][i] + dp[i + 1][b] + x

二叉查找树(binary search tree)详解

二叉查找树(Binary Search Tree),也称二叉排序树(binary sorted tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值 任意节点的左.右子树也分别为二叉查找树 没有键值相等的节点(no duplicate nodes) 本文地址:http://www.cnblogs.com/archimedes/p/binary-search-tree

[LeetCode]235.Lowest Common Ancestor of a Binary Search Tree

题目 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that h

[LeetCode]109.Convert Sorted List to Binary Search Tree

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 无 [代码] 在下面超时的代码上进行改进:把链表先转换为vector再进行操作. [LeetCode]108.Convert Sorted Array to Binary Search Tree /*******************************

Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?   confused what "{1,#,2,3}&qu

[LeetCode]173.Binary Search Tree Iterator

[题目] Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and

[LeetCode]99.Recover Binary Search Tree

[题目] Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? [解析] O(n) 空间的解法是,开一个指针数组