poj1390 2010.8.1

Blocks

Time Limit: 5000MS  Memory Limit: 65536K

Total Submissions: 1916  Accepted: 734

 

Description

 

Some of you may have played a game called'Blocks'. There are n blocks in a row, each box has a color. Here is anexample: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.

The corresponding picture will be as shownbelow:

 

Figure 1

If some adjacent boxes are all of the samecolor, and both the box to its left(if it exists) and its right(if it exists)are of some other color, we call it a 'box segment'. There are 4 box segments.That is: gold, silver, bronze, gold. There are 1, 4, 3, 1 box(es) in thesegments respectively.

 

Every time, you can click a box, then thewhole segment containing that box DISAPPEARS. If that segment is composed of kboxes, you will get k*k points. for example, if you click on a silver box, thesilver segment disappears, you got 4*4=16 points.

 

Now let's look at the picture below:

 

Figure 2

 

The first one is OPTIMAL.

 

Find the highest score you can get, givenan initial state of this game.

Input

 

The first line contains the number of testst(1<=t<=15). Each case contains two lines. The first line contains aninteger n(1<=n<=200), the number of boxes. The second line contains nintegers, representing the colors of each box. The integers are in the range1~n.

Output

 

For each test case, print the case numberand the highest possible score.

Sample Input

 

2

9

1 2 2 2 2 3 3 3 1

1

1

Sample Output

 

Case 1: 29

Case 2: 1

Source

 

Liu Rujia@POJ

 

 

详见刘汝佳的黑书。。我好像隐隐约约知道什么叫记忆化搜索了。。。。。。

 

 

Source Code

 

Problem: 1390  User: creamxcream

Memory: 36416K  Time: 1579MS

Language: C++  Result: Accepted

 

 

Source Code 

#include <cstdio>
#include <cstring>

#define MAXN 210
#define INF -1
int color[MAXN],len[MAXN];
int n,num;
int f[MAXN][MAXN][MAXN];

void init()
{
 memset(f,-1,sizeof(f));
 scanf("%d",&n);
 int temp;
 scanf("%d",&temp);
 color[1]=temp;
 len[1]=1;
 num=1;
 for(int i=2;i<=n;i++)
 {
  scanf("%d",&temp);
  if (temp==color[num])
   len[num]++;
  else
  {
   num++;
   color[num]=temp;
   len[num]=1;
  }
 }
}

int dpit(int i,int j,int k)
{
 if (f[i][j][k]!=-1)
  return f[i][j][k];
 if (i==j+1) return 0;
 int ans=INF;
 int temp=dpit(i,j-1,0)+(len[j]+k)*(len[j]+k);
 if (temp>ans)
  ans=temp;
 for(int p=i;p<j;p++)
  if (color[p]==color[j])
  {
   temp=dpit(i,p,len[j]+k)+dpit(p+1,j-1,0);
   if (temp>ans)
    ans=temp;
  }
 f[i][j][k]=ans;
 return ans;
}

int main()
{
 int t;
 scanf("%d",&t);
 for (int i=1;i<=t;i++)
 {
  init();
  int ans=dpit(1,num,0);
  printf("Case %d: %d\n",i,ans);
 }
 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值