UVA - 10304Optimal Binary Search Tree(递推)

题目:UVA - 10304Optimal Binary Search Tree(递推)

题目大意:给出一组数,e1 < e2 < ... < en,现在要求将这些数组成一棵二叉搜索树,并且使得sum (ei * cost(ei))最小。cost(ei)表示ei到到根节点之间有多少条边。

解题思路:首先二叉搜索树要满足左节点小于根节点,右节点大于根节点。因此对于e1 < e2 < ... < en这样一组数,我们只要枚举根节点的位置ek,将这个序列分成左右子树两部分(e1,e2..ek-1) (ek+ 1, ek + 2, ..en)。然后找到最优的根节点。而且对于两个子树,也是同样的做法(最优子结构)。

状态转移方程:dp【i】【j】 = MIN (dp【i】【k - 1】 + dp【k +1】【j】 + num【k - 1】 - num【i - 1】 + num【j】 - num【k】) 。

代码:

#include <cstdio>
#include <cstring>

const int N = 255;
const int INF = 0x3f3f3f3f;

int v[N];
int num[N];
int dp[N][N];
int n;

int Min (const int a, const int b) { return a < b? a: b; }

int main () {

	while (scanf ("%d", &n) != EOF) {

		num[0] = 0;
		for (int i = 1; i <= n; i++) {

			scanf ("%d", &v[i]);
			num[i] = num[i - 1] + v[i];
		}

		for (int i = 1; i <= n; i++)
			dp[i][i] = 0;

		int left, right;
		for (int l = 1; l < n; l++)
			for (int i = 1; i + l <= n; i++) {

				dp[i][i + l] = INF;
				for (int j = i; j <= i + l; j++) {

					if (j != i)
						left = dp[i][j - 1] + num[j - 1] - num[i - 1];
					else
						left = 0;

					if (j != i + l)
						right = dp[j + 1][i + l] + num[i + l] - num[j];
					else
						right = 0;

					dp[i][i + l] = Min (dp[i][i + l], left + right);
				}
			}

//		printf ("%d %d\n", INF, 1<<30);
		printf ("%d\n", dp[1][n]);
	}
	return 0;
}

UVA - 10304Optimal Binary Search Tree(递推),布布扣,bubuko.com

时间: 2024-12-24 16:42:50

UVA - 10304Optimal Binary Search Tree(递推)的相关文章

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

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

UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以 代码: #include <cstdio> #include <cstring> typedef long long ll; const int MAXNODE =

uva 10304 Optimal Binary Search Tree (区间DP)

uva 10304 Optimal Binary Search Tree 题目大意:给出N个结点(已知每个结点的权值)来建树,建树时要满足以下规则:左子树的节点的值要全小于父节点,右子树的节点的值要全大于父节点.要求最后建出的树总权值最小.总权值=各结点乘以层数(从0层开始)之后相加的和. 解题思路:dp[i][j]代表区间第i个结点到第j个结点组成的树最小的总权值.dp[j][i]=min(dp[j][i],dp[j][k?1]+dp[k+1][i]+sum[i]?sum[j?1]?num[k

leetcode -day27 Recover Binary Search Tree &amp; Interleaving String

1.  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 solut

[LeetCode] Validate Binary Search Tree 验证二叉搜索树

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

[LeetCode] 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}"

72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / ___2__ ___8__ / \ / 0 _4 7 9 / 3 5 例如: 2,8 -->6 2,4-–>2 原文描述: 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

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 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 th