zoukankan      html  css  js  c++  java
  • 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

    嗯--dp。。。。
    学习的题解:http://blog.csdn.net/lg_csu/article/details/17084449
    题意:求n个节点能构成多少个不同的高度不小于h的二叉树。
    解题思路:从n=1考虑,只有一个,再往后都是在前面的基础上进行的。左二叉树的种类与右二叉树的种类数量相乘就是新的二叉树的数量。
    附ac代码:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <cstring>
     5 #include <fstream>
     6 #include <algorithm>
     7 #include <cmath>
     8 #include <queue>
     9 #include <stack>
    10 #include <vector>
    11 #include <map>
    12 #include <set>
    13 #include <iomanip>
    14 using namespace std;
    15 typedef long long ll;
    16 const int maxn = 44;
    17 ll dp[maxn][maxn];
    18 int main()
    19 {
    20     ios::sync_with_stdio(false);
    21     ll n,h;
    22     cin>>n>>h;
    23     for(int i=0;i<=35;++i) dp[0][i]=1;  //从0开始便于转移公式从1开始进行。
    24     for(int i=1;i<=35;++i)
    25     {
    26         for(int j=1;j<=35;++j)
    27         {
    28             for(int k=0;k<i;++k)
    29             {
    30                 dp[i][j]+=dp[k][j-1]*dp[i-k-1][j-1];
    31             }
    32         }
    33     }
    34     cout<<dp[n][n]-dp[n][h-1]<<endl;
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    linux 常用快捷键
    命令行远程链接
    mybatis参数错误 Parameter '×××' not found. Available parameters are [0, 1, param1, param2]
    mybatis延迟加载
    mybatis跨XML引用
    eclipse修改项目名称
    wait, WIFEXITED, WEXITSTATUS
    进程通信中如何进行值得传递?
    fork新建进程
    Ubuntu安装genymotion模拟器步骤
  • 原文地址:https://www.cnblogs.com/zmin/p/8390541.html
Copyright © 2011-2022 走看看