加分二叉树

题目描述

设一个n个结点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为结点编号。每个结点都有一个分数(均为正整数),记第i个结点的分数为di,tree及它的每个子树都有一个加分,任一棵子树subtree(也包含tree本身)的加分计算方法如下:

subtree的左子树的加分×subtree的右子树的加分+subtree的根的分数。subtree的左子树的加分×subtree的右子树的加分+subtree的根的分数。

若某个子树为空,规定其加分为1,叶子的加分就是叶结点本身的分数。不考虑它的空子树。

试求一棵符合中序遍历为(1,2,3,…,n)且加分最高的二叉树tree。要求输出;

  1. tree的最高加分
  2. tree的前序遍历

输入格式

第1行:一个整数n(n<30),为结点个数。 
第2行:n个用空格隔开的整数,为每个结点的分数(分数<100)。

输出格式

第1行:一个整数,为最高加分(结果不会超过4,000,000,000)。 
第2行:n个用空格隔开的整数,为该树的前序遍历。

输入样例


5 7 1 2 10

输出样例

145 
3 1 2 4 5

Sol

不妨开两个数组。

  • f[l,r]为从l结点到r结点的最大分数。f[l,r]为从l结点到r结点的最大分数。
  • root[l,r]为当[l,r]这个结点取最大分值时,它的树根结点。root[l,r]为当[l,r]这个结点取最大分值时,它的树根结点。此举有利于进行DFS操作。

中序: 
  根 
  / \ 
 小 大

代码

#include <cstdio>
#include <cstring>
using namespace std;

int f[30][30], root[30][30];
int n;

int get(int l, int r) {
    if (l > r) return 1;//空子树的值为1,且1对乘积无影响,可直接取1
    if (~f[l][r]) return f[l][r];//如果已经遍历,则可以直接返回值
    for (int k = l; k <= r; k++) {//枚举每个结点为树根的可能性,找出最大的分值
        int now = get(l, k - 1) * get(k + 1, r) + f[k][k];
        if (now > f[l][r]) {//找到比当前更大的分值就更新,同时记录树根节点
            f[l][r] = now;
            root[l][r] = k;
        }
    }
    return f[l][r];//返回最大分值
}

void dfs(int l, int r) {
    if (l > r) return;
    printf("%d ", root[l][r]);
    dfs(l, root[l][r] - 1);
    dfs(root[l][r] + 1, r);
    //用root数组将原树分段,并以前序遍历打印
}

int main(int argc, char **argv) {
    scanf("%d", &n);

    //Initialize
    memset(f, -1, sizeof f);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &f[i][i]);//读入每个结点的分值
        root[i][i] = i;//自己的根结点是自己
    }

    //开始分治
    printf("%d\n", get(1, n));

    //打印前序遍历
    dfs(1, n);
}
posted @ 2018-08-16 23:02  strawqqhat  阅读(139)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }