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;
}
#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;
}
相关文章推荐
- poj 1390 Blocks
- [poj1390]Blocks(方块消除)
- poj 1390 Blocks (区间dp)
- POJ 1390 Blocks (DP)
- [POJ1390]Blocks(dp)
- POJ1390 Blocks 【动态规划】
- POJ - 1390 Blocks
- poj 1390 Blocks(dp,黑书dp方块消除)
- POJ 1390 Blocks (区间DP)
- poj1390 Blocks
- POJ1390 Blocks 【动态规划】
- POJ 1390 Blocks(DP)
- poj 1390 Blocks(记忆化搜索/动态规划)
- poj-1390-Blocks(方块消除)-DP
- POJ 1390 Blocks
- [POJ1390]Blocks(区间dp)
- poj 1390:Blocks 方盒游戏
- poj 1390 Blocks (记忆化搜索)
- POJ 1390 Blocks 笔记
- poj 1390 Blocks