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 = 1061109567;
int dp[260][260], a[260], pre[260];
int dfs(int l, int r)
{
    if(~dp[l][r])
        return dp[l][r];
    if(l >= r)
        return dp[l][r] = 0;
    if(l == r - 1) {
        return dp[l][r] = min(a[l], a[r]);
    }
    dp[l][r] = inf;
    for(int i = l-1; i < r; i++) {
        dp[l][r] = min(dp[l][r], dfs(l, i) + dfs(i + 2, r) + pre[r]-pre[l-1] - a[i+1]);
    }
    return dp[l][r];
}
int main()
{
    int n;

    while(~scanf("%d", &n)) {
        mem1(dp);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            pre[i] = pre[i-1] + a[i];
        }
        printf("%d\n", dfs(1, n));
    }
    return 0;
}
时间: 2024-10-25 12:16:23

uva 10304 - Optimal Binary Search Tree 区间dp的相关文章

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

UVA 10304 Optimal Binary Search Tree

简单区间DP. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn = 300; int n; int a[maxn], su

[Coding Made Simple] Optimal Binary Search Tree

Given keys and frequency at which these keys are searched, how would you create a binary search tree from these keys such that the cost of searching is minimum. The cost of searching is defined as the sum of all node's search frequency * its depth; R

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 optimal binary search tree with keys ki ... kr-1 as its left subtree and an optimal binary search tree with keys kr+1 ... kj as its right subtree.What happens

UVA - 10304Optimal Binary Search Tree(递推)

题目:UVA - 10304Optimal Binary Search Tree(递推) 题目大意:给出一组数,e1 < e2 < ... < en,现在要求将这些数组成一棵二叉搜索树,并且使得sum (ei * cost(ei))最小.cost(ei)表示ei到到根节点之间有多少条边. 解题思路:首先二叉搜索树要满足左节点小于根节点,右节点大于根节点.因此对于e1 < e2 < ... < en这样一组数,我们只要枚举根节点的位置ek,将这个序列分成左右子树两部分(e

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 =

LeetCode: Validate Binary Search Tree [098]

[题目] 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

2018.3.5-6 knapsack problem, sequence alignment and optimal binary search trees

这周继续dynamic programming,这三个算法都是dynamic programming的. knapsack problem有一种greedy的解法,虽然简单但是不保证正确,这里光头哥讲的是dynamic的解法.其实和上次那个max weight independent set的算法差不多,同样是每个物件都判断一遍有这个物件和没这个物件两种情况,用bottom-up的方法来解,然后得到一个最大的value值,这时因为没有得到具体的选择方案,所以最后还需要一部重构的步骤得到具体方案.