您的位置:首页 > 其它

Blocks UVA - 10559

2017-09-28 23:00 429 查看
解题思路本身不复杂,但是难想到,考虑的点主要在于对于一部分已知的颜色,有两种选择:第一种是将这一部分的颜色直接全部消除,立即得到相应的分数之后再接着进行后续的分析操作;第二种是尽量将这种颜色的部分进行拼接(大意是这样,可能不太准确),然后进行消除,对于这两种选择,选择最大的结果作为最终的结果,紫书中作者给出了一种很巧妙的记录方式,记录的是相应的下标以及右下标的右侧颜色相同的块的个数来解决这个问题的,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;

int t;
int color[210];
int result[210][210][210];

int dp(int i, int j,int k){
if (i > j) return 0;
int& res = result[i][j][k];
if (res >= 0) return res;
int p = j;
while (i <= p&&color[p] == color[j]) p--;
p++;
res = dp(i, p - 1, 0) + (j - p + 1 + k)*(j - p + 1 + k);
for (int q = i; q < p; q++){
if (color[q] == color[j] && color[q] != color[q + 1])
res = max(res, dp(q + 1, p - 1, 0) + dp(i, q, j - p + 1 + k));
}
return res;
}

int main(){
cin >> t;
int Case = 0;
while (t--){
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> color[i];
memset(result,-1,sizeof(result));
cout << "Case " << ++Case << ": " << dp(0, n - 1, 0) << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: