P3162
考虑 区间DP
(dp[l][r] = max(dp[l][k] + 1) \,if(dp[l][k] == dp[k + 1][r])
l~k
这段区间和k+1,r
这段区间的值如果是相同的,那么可以合并成l~r
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define rep(I, N) for (int I = 1; I <= (N); ++I)
#define repp(I, N) for (int I = 0; I < (N); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORR(I, A, B) for (int I = (A); I >= (B); I--)
#define SORT_UNIQUE(c) (sort(c.begin(), c.end()), c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
const int N = 1000 + 5;
const double eps = 1e-7;
const int mod = 1e9 + 7;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
LL gcd(LL a, LL b) { return b > 0 ? gcd(b, a % b) : a; }
LL ksm(LL a, LL b)
{
LL ans = 1;
while (b)
{
if (b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans % mod;
}
int a[N];
int dp[N][N];
int dfs(int l, int r)
{
if (dp[l][r] != -1)
return dp[l][r];
if (l == r)
return dp[l][r] = a[l];
int ans = 0;
for (int k = l; k < r; k++)
{
int t1 = dfs(l, k);
int t2 = dfs(k + 1, r);
if (t1 == t2)
ans = max(ans, t1 + 1);
}
return dp[l][r] = ans;
}
int main()
{
int n;
cin >> n;
rep(i, n) cin >> a[i];
memset(dp, -1, sizeof dp);
dfs(1, n);
int ans = 0;
for (int i = 1; i <= n;i++)
{
for (int j = i; j <= n;j++)
ans = max(ans, dp[i][j]);
}
cout << ans << endl;
return 0;
}