zoukankan      html  css  js  c++  java
  • POJ 1095 Trees Made to Order(卡特兰数列)

    题目链接

    中间计算的各种细节。有的细节没处理好,就wa了。。。主要思路就是根据卡特兰数列的:

    h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)

     1 #include <cstdio>
     2 #include <string>
     3 #include <cstring>
     4 #include <queue>
     5 #include <map>
     6 #include <iostream>
     7 #include <algorithm>
     8 using namespace std;
     9 #define LL __int64
    10 LL ctl[101];
    11 LL sum[101];
    12 struct node
    13 {
    14     int l,r;
    15 }tree[200001];
    16 int t;
    17 void build(LL n,int rt)
    18 {
    19     int i,pos;
    20     if(n == 1||n == 0)
    21     {
    22         return ;
    23     }
    24     for(i = 1;i <= 20;i ++)
    25     {
    26         if(sum[i] >= n)
    27         break;
    28     }
    29     pos = i;
    30     n -= sum[pos-1];
    31     for(i = 0;i <= pos-1;i ++)
    32     {
    33         if(n <= ctl[i]*ctl[pos-1-i])
    34         break;
    35         n -= ctl[i]*ctl[pos-1-i];
    36     }
    37     if(i != 0)
    38     {
    39         tree[rt].l = t ++;
    40         if(n%ctl[pos-1-i] != 0)
    41         build(sum[i-1]+n/ctl[pos-1-i]+1,t-1);
    42         else
    43         build(sum[i-1]+n/ctl[pos-1-i],t-1);
    44     }
    45     if(i != pos-1)
    46     {
    47         tree[rt].r = t ++;
    48         if(n%ctl[pos-1-i] == 0)
    49         build(sum[pos-2-i]+ctl[pos-1-i],t-1);
    50         else
    51         build(sum[pos-2-i]+n%ctl[pos-1-i],t-1);
    52     }
    53 }
    54 void show(int x)
    55 {
    56     if(x == -1)
    57     return ;
    58     if(tree[x].l != -1)
    59     printf("(");
    60     show(tree[x].l);
    61     if(tree[x].l != -1)
    62     printf(")");
    63     printf("X");
    64     if(tree[x].r != -1)
    65     printf("(");
    66     show(tree[x].r);
    67     if(tree[x].r != -1)
    68     printf(")");
    69 
    70 }
    71 int main()
    72 {
    73     int i;
    74     LL n;
    75     ctl[0] = 1;
    76     ctl[1] = 1;
    77     sum[1] = 1;
    78     for(i = 2;i <= 20;i ++)
    79     {
    80         ctl[i] = ctl[i-1]*(4*i-2)/(i+1);
    81         sum[i] = sum[i-1] + ctl[i];
    82     }
    83     while(cin>>n)
    84     {
    85         if(n == 0) break;
    86         t = 2;
    87         for(i = 1;i <= 200000;i ++)
    88         tree[i].l = tree[i].r = -1;
    89         build(n,1);
    90         show(1);
    91         printf("
    ");
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    pgrep 查询进程的工具
    shell脚本中>/dev/null的含义
    一条命令批量替换多个文件中字符串
    Centos 6.4上面用Shell脚本一键安装vsftpd
    centos 卸载vsftpd方法
    Centos 6.4上面用Shell脚本一键安装mysql 5.6.15
    SHELL学习笔记----IF条件判断,判断条件
    CentOS挂载新硬盘
    Linux fdisk 命令
    Linux df 命令
  • 原文地址:https://www.cnblogs.com/naix-x/p/3248690.html
Copyright © 2011-2022 走看看