0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

P3607 [USACO17JAN]Subsequence Reversal P

https://www.luogu.com.cn/record/86419056
This commit is contained in:
Baoshuo Ren 2022-09-12 08:51:28 +08:00
parent 3e05d6d6b5
commit fff2f5cd1d
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

57
Luogu/P3607/P3607.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <algorithm>
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;
}