zoukankan      html  css  js  c++  java
  • 出栈 记忆搜索+卡特兰数

    https://www.luogu.org/problemnew/show/P1044

    1.用dfs来做,记录以备用

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<stdlib.h>
     8 #define mem(a) memset(a,0,sizeof(a))
     9 using namespace std;
    10 long f[20][20];
    11 long dfs(int x,int y) //x外,y内
    12 {
    13     if(f[x][y]!=0) return f[x][y];
    14     else
    15     {
    16       if(x==0)return 1; //外面没有后,只有栈里有,也只有一种可能
    17       if(y>0)  f[x][y]+=dfs(x,y-1); //从栈出
    18       f[x][y]+=dfs(x-1,y+1); //从外入
    19       return f[x][y];
    20     }
    21 
    22 }
    23 int main()
    24 {
    25   int n;
    26   mem(f);
    27   cin>>n;
    28   cout<<dfs(n,0)<<endl;
    29   return 0;
    30 }
    View Code

    2.卡特兰数

    设某位置为k,则比k早进栈且早出栈的有k-1个数,则有h(k-1)种可能,同理比k晚的有h(n-k)方案。 所以

    h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0)=c(2n,n)-c(2n,n+1)(n=0,1,2,...)=C(2n,n)/(n+1)   //k的所有位置的可能性

    //如果把数组定义在main里面 要初始化

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 #include<stdlib.h>
     8 #define mem(a) memset(a,0,sizeof(a))
     9 using namespace std;
    10 int f[20];
    11 int main()
    12 {
    13   int n;
    14   scanf("%d",&n);
    15   f[0]=1; f[1]=1;
    16   for(int i=2;i<=n;i++)
    17   {
    18       for(int j=0;j<i;j++)
    19         f[i]+=f[j]*f[i-j-1];
    20   }
    21   printf("%d
    ",f[n]);
    22   return 0;
    23 }
    View Code
  • 相关阅读:
    bzoj 1195: [HNOI2006]最短母串 爆搜
    bzoj 4066: 简单题 kd-tree
    NOI冲刺计划2
    bzoj 3572: [Hnoi2014]世界树 虚树 && AC500
    bzoj 3153: Sone1 Toptree
    CTSC && APIO 总结
    bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp
    bzoj 1902: Zju2116 Christopher lucas定理 && 数位DP
    BZOJ 1754: [Usaco2005 qua]Bull Math
    BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
  • 原文地址:https://www.cnblogs.com/XXrll/p/10224454.html
Copyright © 2011-2022 走看看