DP专题练习 toasting
区间dp
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+7; const int N=1550; int n; int f[maxn]; int dp[N][N]; int main() { freopen("toasting.in","r",stdin); freopen("toasting.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&f[i]); } for(int i=2;i<=n;++i){ for(int j=i-1;j>0;j-=2){ for(int k=j;k<i;k+=2){ int ans=0; if(f[i]==f[k]){ ans=1; } dp[j][i]=max(dp[j][i],dp[j][k-1]+dp[k+1][i-1]+ans); } } } printf("%d",dp[1][n]); return 0; }