解析
很巧妙的题
我一开始的dp设计其实是可以的
只是我误认为它的转移需要n^2
然后尝试把它压掉一维结果越跑越远…
但一个技巧是只从相邻的状态转移
使转移变成O1
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=52;
const int M=1e6+100;
ll read(){ll x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){x=x*10+(c^48);c=getchar();}return x*f;
}inline void Max(int &x,int y){if(x<y) x=y;}int n;
int dp[N][N][N][N];
int a[N];int main(){#ifndef ONLINE_JUDGEfreopen("a.in","r",stdin);freopen("a.out","w",stdout);#endifn=read();for(int i=1;i<=n;i++) a[i]=read();for(int len=1;len<=n;len++){for(int l=1;l+len-1<=n;l++){int r=l+len-1;for(int st=50;st>=1;st--){for(int ed=st;ed<=50;ed++){Max(dp[l][r][st][ed],dp[l][r][st+1][ed]);Max(dp[l][r][st][ed],dp[l][r][st][ed-1]);Max(dp[l][r][st][ed],dp[l+1][r][st][ed]+(a[l]==st));//if(dp[l][r][st][ed]) printf("1:(%d %d) dp=%d\n",st,ed,dp[l][r][st][ed]);Max(dp[l][r][st][ed],dp[l][r-1][st][ed]+(a[r]==ed));//if(dp[l][r][st][ed]) printf("2:(%d %d) dp=%d\n",st,ed,dp[l][r][st][ed]);if(l!=r) Max(dp[l][r][st][ed],dp[l+1][r-1][st][ed]+(a[r]==st)+(a[l]==ed));//if(dp[l][r][st][ed]) printf("3:(%d %d) dp=%d\n",st,ed,dp[l][r][st][ed]);}}}}//fprintf(stderr,"check=%d\n",dp[1][1][1][1]);printf("%d\n",dp[1][n][1][50]);
}
/**/