题意:一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数。求最大分数是多少。
析:dp[i][j][k] 表示消除 i ~ j,并且右边再拼上 k 个 颜色等于a[j] 的方块所以得到的新序列的最大得分,也就是说那 k 个是来自右边,我们已经消除了它们之间的其他方块才得到的。
这样的话有两种决策:
第一种,直接消除最右边的段,也就是转移到 dp[i][p-1] + (j-p+k+1)^2,其中 p 表示从 j 最远可延伸到 p,使得其中的a[x] == a[j]。
第二种,枚举和左边哪一段拼接起来,也就是找 q < p && a[q] == a[r] && a[q+1] != a[q],这样的话就转移到 dp[q+1][p-1][0] + dp[i][q][j-p+k+1]。
记忆化搜索即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 10;
const int maxm = 1e6 + 10;
const int mod = 100007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int a[maxn];
int dp[maxn][maxn][maxn];
int LEFT[maxn]; int dfs(int l, int r, int k){
int &ans = dp[l][r][k];
if(ans >= 0) return ans;
if(r < l) return ans = 0;
if(LEFT[r] == l) return ans = sqr(r - l + k + 1);
ans = dfs(l, LEFT[r]-1, 0) + sqr(r - LEFT[r] + 1 + k);
for(int i = LEFT[r]-2; i >= l; --i){ // take care of the lower_bound
if(a[i] == a[r]){
ans = max(ans, dfs(l, i, r - LEFT[r] + 1 + k) + dfs(i+1, LEFT[r]-1, 0));
i = LEFT[i] - 1;
}
}
return ans;
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", a+i);
int j = i;
while(a[j-1] == a[i]) --j;
LEFT[i] = j;
}
ms(dp, -1); dp[0][0][0] = 0;
printf("Case %d: %d\n", kase, dfs(1, n, 0));
}
return 0;
}