Blocks
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4744   Accepted: 1930

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 an example: Gold, Silver, Silver, Silver, Silver, Bronze, Bronze, Bronze, Gold.
The
corresponding picture will be as shown below:

Figure 1
If some adjacent
boxes are all of the same color, 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 the segments respectively.

Every time, you can click a
box, then the whole segment containing that box DISAPPEARS. If that segment is
composed of k boxes, you will get k*k points. for example, if you click on a
silver box, the silver 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, given an initial state
of this game.

Input

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

Output

For each test case, print the case number and 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
题意:通过点击某一颜色消除相邻的所有的这种颜色,得分为len*len,求最大分;
http://wenku.baidu.com/view/d956d2f30b4c2e3f5627630b分析:当看代码的时候却觉着原来就是这么简单
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
struct box_segment
{
int color,len;
};
box_segment segment[];
int score[][][];
int click_score(int start,int End,int extra_len)
{
if(score[start][End][extra_len] > )
return score[start][End][extra_len];
int result;
result = segment[End].len + extra_len;
result = result * result;
if(start == End)
{
score[start][End][extra_len] = result;
return score[start][End][extra_len];
}
result += click_score(start, End - , );
for(int i = End - ; i >= start; i--)
{
if(segment[i].color != segment[End].color)
continue;
int temp = click_score(start, i, segment[End].len + extra_len) + click_score(i + , End - ,);
if(temp <= result)
continue;
result = temp;
break;
}
score[start][End][extra_len] = result;
return score[start][End][extra_len];
}
int main()
{
int t,n,End;
int num = ;
scanf("%d", &t);
while(t--)
{
End = ;
scanf("%d", &n);
scanf("%d", &segment[End].color);
segment[End].len = ;
for(int i = ; i < n; i ++)
{
int color;
scanf("%d", &color);
if(color == segment[End].color)
segment[End].len++;
else
{
segment[++End].color = color;
segment[End].len = ;
}
}
memset(score, , sizeof(score));
printf("Case %d: %d\n", ++num,click_score(, End, ));
}
return ;
}

最新文章

  1. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
  2. Android基础总结(四)
  3. 最近开始研究PMD(一款采用BSD协议发布的Java程序代码检查工具)
  4. ASP.NET C# 文件下载
  5. flash memory
  6. ecshop二次开发 给商品添加自定义字段
  7. Redis 数据序列化方法 serialize, msgpack, json, hprose 比较
  8. vs2015官方下载链接
  9. mongo-spark-读取不同的库数据和写入不同的库中
  10. 洛谷P4117 [Ynoi2018]五彩斑斓的世界 [分块,并查集]
  11. Appium 连手机失败Error: Android bootstrap socket crashed: Error: getaddrinfo ENOTFOUND localhost undefined:4724
  12. 【第二十五章】 springboot + hystrixdashboard
  13. mybatis待研究
  14. C# 文件下载断点续传
  15. IO多路复用之epoll(二)
  16. centos 安装php7
  17. hdu 4198:Quick out of the Harbour解题报告
  18. Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)
  19. MySql中的锁(表锁,行锁)
  20. SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(一)

热门文章

  1. 深入理解javascript函数参数与闭包(一)
  2. 便于开发的Helper类
  3. iOS角度与弧度转换
  4. android 自定义通知栏
  5. Myeclipse开发环境下文件中出现的提示错误与解决方法:The import javax.servlet cannot be resolved?
  6. 安卓APP与智能硬件相结合的简易方案
  7. 跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题
  8. MFC消息映射机制以及画线功能实现
  9. 可跨域的单点登录(SSO)实现方案【附.net代码】
  10. 【Quartz】将定时任务持久化到数据库