From fff2f5cd1de129212c109f3c21689aece9438185 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 12 Sep 2022 08:51:28 +0800 Subject: [PATCH] P3607 [USACO17JAN]Subsequence Reversal P https://www.luogu.com.cn/record/86419056 --- Luogu/P3607/P3607.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Luogu/P3607/P3607.cpp diff --git a/Luogu/P3607/P3607.cpp b/Luogu/P3607/P3607.cpp new file mode 100644 index 00000000..a756fdad --- /dev/null +++ b/Luogu/P3607/P3607.cpp @@ -0,0 +1,57 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 55; + +int n, a[N], f[N][N][N][N]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + for (int l = 1; l <= n; l++) { + for (int i = 1; i <= a[l]; i++) { + for (int j = a[l]; j <= 50; j++) { + f[l][l][i][j] = 1; + } + } + } + + for (int len = 2; len <= n; len++) { + for (int l = 1, r = len; r <= n; l++, r++) { + for (int i = 1; i <= 50; i++) { + for (int j = i; j <= 50; j++) { + f[l][r][i][j] = std::max({ + f[l][r][i][j], + f[l + 1][r][i][j] + (a[l] == i), + f[l][r - 1][i][j] + (a[r] == j), + f[l + 1][r - 1][i][j] + (a[r] == i) + (a[l] == j), + }); + + f[l][r][i][j + 1] = std::max(f[l][r][i][j + 1], f[l][r][i][j]); + f[l][r][i - 1][j] = std::max(f[l][r][i - 1][j], f[l][r][i][j]); + } + } + + for (int j = 1; j <= 50; j++) { + for (int i = j; i; i--) { + f[l][r][i - 1][j] = std::max(f[l][r][i - 1][j], f[l][r][i][j]); + } + } + } + } + + cout << f[1][n][1][50] << endl; + + return 0; +}