洛谷P1044 [NOIP2003 普及组] 栈(DP、卡特兰数、递推)

DP:

#include <bits/stdc++.h>
using namespace std;

int p[20][20];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i <= n; ++i) {
        p[i][0] = 1;
    }
    for (int j = 1; j <= n; ++j) {
        for (int i = 0; i <= n; ++i) {
            if (i >= 1) {
                p[i][j] = p[i - 1][j] + p[i + 1][j - 1];
            }
            if (i == 0) {
                p[i][j] = p[i + 1][j - 1];
            }
        }
    }
    cout << p[0][n] << endl;
    return 0;
}

卡特兰数:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ULL unsigned long long

int n;
int p[20];
int main() {
    cin >> n;
    p[0] = p[1] = 1;
    for (int i = 2; i <= n; ++i) {
        for (int j = 0; j < n; ++j) {
            p[i] += p[j] * p[i - j - 1];
        }
    }
    cout << p[n] << endl;
    return 0;
}
上一篇:洛谷 P1042 [NOIP2003 普及组] 乒乓球 题解 C/C++


下一篇:P1043 [NOIP2003 普及组] 数字游戏 题解(区间dp)