uva 10304 Optimal Binary Search Tree(dp)
2014-08-23 12:02
1506 查看
Optimal Binary Search Tree
Input: standard input
Output: standard output
Time Limit: 30seconds
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.
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.
(The Joint Effort Contest, Problem setter: Rodrigo Malta Schmidt)
根据二叉搜索树的性质,容易想到枚举树根,然后比树根小的都会放在左子树,比树根大的都在右子树,因为它们层数都加1,所以消费加上它们的和,剩下的就是求解左右子树的最优解了,区间dp。
Input: standard input
Output: standard output
Time Limit: 30seconds
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 101010
3 5 1020
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;
}
相关文章推荐
- UVA 10304 Optimal Binary Search Tree (区间dp)
- uva 10304 - Optimal Binary Search Tree(区间dp)
- UVA - 10304 Optimal Binary Search Tree 区间DP
- uva 10304 Optimal Binary Search Tree(区间dp)
- UVa 10304 Optimal Binary Search Tree(区间DP)
- uva 10304 Optimal Binary Search Tree (区间DP)
- 区间dp(uva10304-Optimal Binary Search Tree)
- UVa 10304 Optimal Binary Search Tree / 区间DP
- uva 10304 Optimal Binary Search Tree(DP)
- uva-10304 Optimal Binary Search Tree(区间dp)
- uva 10304 Optimal Binary Search Tree(区间dp)
- uva 10304 - Optimal Binary Search Tree(区间dp)
- DP优化——四边形不等式 uva 10304 - Optimal Binary Search Tree
- UVA 10304 - Optimal Binary Search Tree
- UVA10304- Optimal Binary Search Tree(最优排序二叉树OBST)
- UVA 10304 Optimal Binary Search Tree
- uva 10304 Optimal Binary Search Tree
- UVA 10304 Optimal Binary Search Tree
- uva 10304 Optimal Binary Search Tree 最优二叉树编码 区间dp
- uva_10304 - Optimal Binary Search Tree(最优搜索二叉树)