• Codeforces 9D


    D. How many trees?
    time limit per test
    1 second
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong — the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure — and then terrible Game Cubes fall down on the city, bringing death to those modules, who cannot win the game...

    For sure, as guard Bob appeared in Mainframe many modules stopped fearing Game Cubes. Because Bob (as he is alive yet) has never been defeated by User, and he always meddles with Game Cubes, because he is programmed to this.

    However, unpleasant situations can happen, when a Game Cube falls down on Lost Angles. Because there lives a nasty virus — Hexadecimal, who is... mmm... very strange. And she likes to play very much. So, willy-nilly, Bob has to play with her first, and then with User.

    This time Hexadecimal invented the following entertainment: Bob has to leap over binary search trees with n nodes. We should remind you that a binary search tree is a binary tree, each node has a distinct key, for each node the following is true: the left sub-tree of a node contains only nodes with keys less than the node's key, the right sub-tree of a node contains only nodes with keys greater than the node's key. All the keys are different positive integer numbers from 1 to n. Each node of such a tree can have up to two children, or have no children at all (in the case when a node is a leaf).

    In Hexadecimal's game all the trees are different, but the height of each is not lower than h. In this problem «height» stands for the maximum amount of nodes on the way from the root to the remotest leaf, the root node and the leaf itself included. When Bob leaps over a tree, it disappears. Bob gets the access to a Cube, when there are no trees left. He knows how many trees he will have to leap over in the worst case. And you?

    Input

    The input data contains two space-separated positive integer numbers n and h (n ≤ 35, h ≤ n).

    Output

    Output one number — the answer to the problem. It is guaranteed that it does not exceed 9·1018.

    Examples
    input
    3 2
    output
    5
    input
    3 3
    output
    4

    题意:问由n个点组成的高度≥h的二叉树的数量。

    题解:DP。首先我们用f[i][j]表示有i个节点,0≤高度≤j的二叉树有多少个(只要有1个节点形态不同就算不同,如果两棵二叉树形状相同但相同位置编号不同,仍视为相同的二叉树)。
    然后我们易知f[0][i](0≤i≤35)的值都应初始化为1。
    对于每一棵二叉树,根节点是固定的。假设当前已枚举到i个节点,高度≤j的情况,分二叉树左右子树情况讨论。
    若左子树有k个节点(0≤k<i),则左子树对答案的贡献为f[k][j-1],同时,右子树对答案的贡献为f[i-k-1][j-1],根据分步乘法原理,对于左子树有k(0≤k<i)个节点时,对答案的贡献是
    f[k][j-1]×f[i-k-1][j-1]。
    再根据分类加法原理,把k从0到i-1枚举一遍就可以得到f[i][j]的最终答案。
    预处理进行完,根据前缀和思想,输出f[n][35]-f[n][h-1]就是题目所求答案。
    时间复杂度O(n3),空间复杂度O(n2),其中n恒等于其最大值35。
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int N=40;
     4 int n,h;
     5 long long f[N][N];
     6 void init()
     7 {
     8     for (int i=0;i<=35;++i)
     9     f[0][i]=1;
    10     for (int i=1;i<=35;++i)
    11     for (int j=1;j<=35;++j)
    12     for (int k=0;k<i;++k)
    13     f[i][j]+=f[k][j-1]*f[i-k-1][j-1];
    14 }
    15 int main()
    16 {
    17     scanf("%d%d",&n,&h);
    18     init();
    19     printf("%lld
    ",f[n][35]-f[n][h-1]);
    20     return 0;
    21 }
    View Code
     
  • 相关阅读:
    数据库之case when + group by 联合使用
    JAVA之JSON对象解析
    oracle导入表数据时遇到外键约束问题导致导入失败
    Oracle之会话阻塞
    cmd的操作命令导出导入.dmp文件
    性能优化理解
    JS原型与原型链的理解
    Promise实现原理
    Js运行机制Event Loop
    JS循环中断与异步for...in forEach map
  • 原文地址:https://www.cnblogs.com/zk1431043937/p/7723770.html
Copyright © 2020-2023  润新知