dp,枚举出所有的方法数目,可以发现,有很多方案数目是可以合并的。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 2e3 + 10;
int dp[N][N][2], f[N][N][2];
int lf[N], rt[N];
int ai[N];
int n;
int main()
{
n = read();
upd(i, 1, n)
{
ai[i] = read();
}
upd(i, 1, n) {
upd(j, i, n)
{
dp[i][j][1] = dp[i][j - 1][1] + (ai[j] == 1);
dp[i][j][2] = dp[i][j - 1][2];
if (ai[j] == 2)dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1]) + 1;
/*cout << "i: " << i << "j: " << j << endl;
printf("%d %d\n", dp[i][j][1], dp[i][j][2]);*/
}
}
dwd(i, n, 1)
{
dwd(j, i, 1)
{
f[i][j][1] = f[i][j + 1][1] +(ai[j] == 1);
f[i][j][2] = f[i][j + 1][2];
if (ai[j] == 2)f[i][j][2] = max(f[i][j + 1][2], f[i][j + 1][1]) + 1;
//cout << "i: " << i << "j: " << j << endl;
//printf("%d %d\n", f[i][j][1], f[i][j][2]);
}
}
int ans = 0;
upd(i, 1, n)
{
upd(j, i, n)
{
ans = max(ans, dp[1][i - 1][1] + f[j][i][1] + dp[j + 1][n][1]);
ans = max(ans, dp[1][i - 1][1] + f[j][i][1] + dp[j + 1][n][2]);
ans = max(ans, dp[1][i - 1][1] + f[j][i][2] + dp[j + 1][n][2]);
//cout << ans << endl;
}
}
cout << ans << endl;
}