Problem 1021 - Binary Tree
Problem 1021 - Binary Tree
Time Limit: 1000MS Memory Limit: 65536KB Difficulty:
Total Submit: 51 Accepted: 21 Special Judge: No
Description
Tiantian is studying “Data Structure” now, and the Binary Tree took her attention. It is a kind of data structure that has the property of tree and with its special structure and operation. For its property: every node(except the root) in the tree has only one parent and no more than two children, we may sometime construct algorithms with the complex of O(nlogn) or O(logn) with the help of it! How to construct a nice binary tree? Here comes the problem.
We have got an ” inorder traversal” sequence of a binary tree, each element in the sequence represents the power of the node. Your task is to build the tree with the maximum cost.
A leaf gets the cost of its own power, and an empty subtree gets the cost of 1.
How to calculate the power for building a binary tree? Take the left subtree and the right subtree with cost of t1, t2, the root power with k .
cost of the binary tree = t1 * t2 + k
Input
The input contains several test cases. Each test case starts with a line containing an integer N (1<=N<=30), which describes the number of the nodes in the binary tree. And then N integers follow in the next line, separated by a space, to describe the N nodes’ power. Each power is a positive integer no larger than 100. The total cost of building the binary tree is no larger than 4,000,000,000.
Output
For each test case, output only one line of one integer, to tell the maximum cost of building the binary tree.
Sample Input
5
5 7 1 2 10
Sample Output
145
Hint
Source
6th Xidian University Collegiate Programming Contest(2008.9)
分析Time Limit: 1000MS Memory Limit: 65536KB Difficulty:
Total Submit: 51 Accepted: 21 Special Judge: No
Description
Tiantian is studying “Data Structure” now, and the Binary Tree took her attention. It is a kind of data structure that has the property of tree and with its special structure and operation. For its property: every node(except the root) in the tree has only one parent and no more than two children, we may sometime construct algorithms with the complex of O(nlogn) or O(logn) with the help of it! How to construct a nice binary tree? Here comes the problem.
We have got an ” inorder traversal” sequence of a binary tree, each element in the sequence represents the power of the node. Your task is to build the tree with the maximum cost.
A leaf gets the cost of its own power, and an empty subtree gets the cost of 1.
How to calculate the power for building a binary tree? Take the left subtree and the right subtree with cost of t1, t2, the root power with k .
cost of the binary tree = t1 * t2 + k
Input
The input contains several test cases. Each test case starts with a line containing an integer N (1<=N<=30), which describes the number of the nodes in the binary tree. And then N integers follow in the next line, separated by a space, to describe the N nodes’ power. Each power is a positive integer no larger than 100. The total cost of building the binary tree is no larger than 4,000,000,000.
Output
For each test case, output only one line of one integer, to tell the maximum cost of building the binary tree.
Sample Input
5
5 7 1 2 10
Sample Output
145
Hint
Source
6th Xidian University Collegiate Programming Contest(2008.9)
计算一个二叉树的最大cost。cost值的定义在题目描述中,这里不再赘述。
题目的输入数据给出的是一棵二叉树的中序遍历序列。具有相同中序遍历序列的N个结点的二叉树大概有N!棵。
所以如果打算穷举所有满足条件的二叉树来计算最大的cost值是不现实的,100的阶乘超过了宇宙中所有原子数目的总和。
还是先从二叉树中序遍历序列的结构分析开始。
对于有n个结点的二叉树的中序遍历序列,如果第i个结点为根结点,那么序列中1 至 (i - 1)结点都是i结点的左子树中的结点,(i + 1)至n结点都是i结点的右子树中的结点。
设f(i, j)表示由中序遍历序列中第i到第j结点构成的子树的cost值
那么f(i, j) = max{f(i, k - 1) * f(k + 1, j) + f(k, k) | i <= k <= j}
分析到这儿,解决这一问题的动态规划算法模型已经出来了。我们只需要选择一个合适的计算顺序计算出f(1,n)即得到最终我们需要的答案。(PS:在我的代码选择的下标是从0开始,即最终的结果为f(0,n-1),因为计算过程很简单,就没有写单独的函数,直接写在main函数里了)
题目的输入数据给出的是一棵二叉树的中序遍历序列。具有相同中序遍历序列的N个结点的二叉树大概有N!棵。
所以如果打算穷举所有满足条件的二叉树来计算最大的cost值是不现实的,100的阶乘超过了宇宙中所有原子数目的总和。
还是先从二叉树中序遍历序列的结构分析开始。
对于有n个结点的二叉树的中序遍历序列,如果第i个结点为根结点,那么序列中1 至 (i - 1)结点都是i结点的左子树中的结点,(i + 1)至n结点都是i结点的右子树中的结点。
设f(i, j)表示由中序遍历序列中第i到第j结点构成的子树的cost值
那么f(i, j) = max{f(i, k - 1) * f(k + 1, j) + f(k, k) | i <= k <= j}
分析到这儿,解决这一问题的动态规划算法模型已经出来了。我们只需要选择一个合适的计算顺序计算出f(1,n)即得到最终我们需要的答案。(PS:在我的代码选择的下标是从0开始,即最终的结果为f(0,n-1),因为计算过程很简单,就没有写单独的函数,直接写在main函数里了)
程序代码:
#include<stdio.h> int main() { unsigned f[100][100]; int n, i, j, k, m, t; for(; scanf("%d", &n) != EOF; printf("%u\n", f[0][n - 1])) { for(i = 0; i < n; i++) scanf("%u", &f[i][i]); for(i = n - 2; i >= 0; i--) for(j = i + 1; j < n; f[i][j++] = m) for(m = 0, k = i; k <= j; k++) if(m < (t = (k <= i ? 1 : f[i][k - 1]) * (k >= j ? 1 : f[k + 1][j]) + f[k][k])) m = t; } return 0; }
重剑无锋,大巧不工