uva 10304 Optimal Binary Search Tree(dp)

Optimal Binary Search Tree

Input: standard input

Output: standard output

Time Limit: 30 seconds

Memory Limit: 32 MB

Given a set S = (e1, e2, ..., en) of n distinct elements such that e1 < e2 < ... < en and considering a binary search tree (see the previous problem) of the elements of S, it is desired that higher the query frequency of an element, closer will it be to the root.

The cost of accessing an element eiof S in a tree (cost(ei)) is equal to the number of edges in the path that connects the root with the node that contains the element. Given the query frequencies of the elements of S(f(e1), f(e2, ..., f(en)), we say that the total cost of a tree is the following summation:

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

In this manner, the tree with the lowest total cost is the one with the best representation for searching elements of S. Because of this, it is called the Optimal Binary Search Tree.

Input

The input will contain several instances, one per line.

Each line will start with a number 1 <= n <= 250, indicating the size of S. Following n, in the same line, there will be n non-negative integers representing the query frequencies of the elements of S: f(e1), f(e2), ..., f(en). 0 <= f(ei) <= 100.� Input is terminated by end of file.

Output

For each instance of the input, you must print a line in the output with the total cost of the Optimal Binary Search Tree.

 

Sample Input

1 5 
3 10 10 10 
3 5 10 20 

 

Sample Output

0 
20 
20 

(The Joint Effort Contest, Problem setter: Rodrigo Malta Schmidt)


根据二叉搜索树的性质,容易想到枚举树根,然后比树根小的都会放在左子树,比树根大的都在右子树,因为它们层数都加1,所以消费加上它们的和,剩下的就是求解左右子树的最优解了,区间dp。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<cmath>
using namespace std;
const int maxn = 1000 + 5;
const int INF = 1e9;
const double eps = 1e-6;
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
#define fi first
#define se second

int e[maxn];
int dp[maxn][maxn];
int sum[maxn];

int dfs(int l, int r){
    if(l >= r)
        return dp[l][r] = 0;
    if(dp[l][r] != -1)
        return dp[l][r];
    int ret = INF;
    ret = min(ret, dfs(l+1, r)+sum[r]-sum[l]);
    ret = min(ret, dfs(l, r-1)+sum[r-1]-sum[l-1]);
    for(int i = l+1;i <= r-1;i++){
        ret = min(ret, dfs(l, i-1)+sum[i-1]-sum[l-1]+dfs(i+1, r)+sum[r]-sum[i]);
    }
    return dp[l][r] = ret;
}

int main(){
    int n;
    while(scanf("%d", &n) != EOF){
        sum[0] = 0;
        for(int i = 1;i <= n;i++){
            scanf("%d", &e[i]);
            sum[i] = sum[i-1]+e[i];
        }
        memset(dp, -1, sizeof dp);

        printf("%d\n", dfs(1, n));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值