BZOJ 1032 [JSOI2007]祖码Zuma

题目

哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈

#include <cstdio>
#include <iostream>
using namespace std;
const int inf = 1000000000;
int n, col[505];
int f[505][505];
bool mark[505][505];
int dp(int l, int r) {
	if(l == r) return 2;
	int & res = f[l][r];
	if(mark[l][r]) return res;
	mark[l][r] = 1;
	res = inf;
	for(int i = l; i < r; i++) res = min(res, dp(l, i) + dp(i + 1, r));
	if(col[l] != col[r]) return res;
	int pos1 = l, pos2 = r;
	while(pos1 <= pos2 && col[pos1 + 1] == col[pos1]) pos1++;
	while(pos1 <= pos2 && col[pos2 - 1] == col[pos2]) pos2--;
	if(pos1 > pos2) return res = 1;
	int k = 0;
	if(pos1 - l + r - pos2 < 1) k = 1;
	res = min(res, dp(pos1 + 1, pos2 - 1) + k);
	return res;
}
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &col[i]);
	printf("%d\n", dp(1, n));
	return 0;
}

posted @ 2018-05-20 21:49  LJZ_C  阅读(123)  评论(0编辑  收藏  举报