您的位置:首页 > 其它

poj 1390 blocks

2013-08-27 14:48 288 查看
刘汝佳黑书上的题目。书上解释的很详细了,不说了。

#include<iostream>
#include<cstring>
using namespace std;
int dp[205][205][205];
int color[205],len[205];
int maxi(int a,int b)
{
if(a>b)
return a;
return b;
}
int dfs(int start,int end,int last)
{
if(dp[start][end][last]>=0)
return dp[start][end][last];
if(start>end)
{
dp[start][end][last]=0;
return dp[start][end][last];
}
if(start==end)
{
dp[start][end][last]=(len[end]+last)*(len[end]+last);
return dp[start][end][last];
}
int ans=dfs(start,end-1,0)+(len[end]+last)*(len[end]+last);
for(int x=end-1;x>=start;x--)
if(color[x]==color[end])
ans=maxi(ans,dfs(start,x,len[end]+last)+dfs(x+1,end-1,0));
dp[start][end][last]=ans;
return dp[start][end][last];
}
int main()
{
int test,count,n,length,i,a,j,k;
cin>>test;
count=0;
while(test--)
{
count=count+1;
color[0]=0;
length=1;
cin>>n;
cin>>color[1];
len[1]=1;
for(i=2;i<=n;i++)
{
cin>>a;
if(a!=color[length])
{
length=length+1;
color[length]=a;
len[length]=1;
}
else len[length]+=1;
}
memset(dp,-1,sizeof(dp));
int ko=dfs(1,length,0);
cout<<"Case "<<count<<": "<<ko<<endl;
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: