Blocks
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 4173   Accepted: 1661

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

题意:给定n个方块,当中有些颜色是连续的,每次点击一个方块就能够消除掉跟它连续的同样的颜色的方块,获得积分为消除长度的平方。给定一个方块序列,求最大能获得多少积分。

题解:状态方程score[i][j][k]为将连续小块统计成大块后从第i个到第j个慷慨块且第j个后面有k个连续的与其同色的方块所获得的最大积分。

我认为有问题的代码。仿照着讲义代码写的。可是也能AC。并且时间消耗是344ms。 应该是那个地方我没理解:

#include <stdio.h>
#include <string.h>
#define maxn 200 struct Node{
int color, len;
} segment[maxn];
int score[maxn][maxn][maxn], arr[maxn]; int clickBox(int left, int right, int exLen)
{
if(score[left][right][exLen]) return score[left][right][exLen];
int i, ans, ans2;
ans = segment[right].len + exLen;
ans = ans * ans;
if(left == right) return score[left][right][exLen] = ans;
ans += clickBox(left, right - 1, 0);
for(i = right - 1; i >= left; --i){
if(segment[i].color != segment[right].color) continue;
ans2 = clickBox(left, i, exLen + segment[right].len) +
clickBox(i + 1, right - 1, 0);
if(ans2 <= ans) continue;
ans = ans2; break;
}
return score[left][right][exLen] = ans;
} int main()
{
int t, n, i, id, cas = 1;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(i = 0; i < n; ++i) scanf("%d", arr + i);
memset(score, 0, sizeof(score));
segment[id = 0].color = arr[0];
segment[id].len = 1;
for(i = 1; i < n; ++i){
if(arr[i] != arr[i-1]){
segment[++id].color = arr[i];
segment[id].len = 1;
}else ++segment[id].len;
}
printf("Case %d: %d\n", cas++, clickBox(0, id, 0));
}
return 0;
}

我认为没问题的代码,时间消耗1688ms:

#include <stdio.h>
#include <string.h>
#define maxn 200 struct Node{
int color, len;
} segment[maxn];
int score[maxn][maxn][maxn], arr[maxn]; int clickBox(int left, int right, int exLen)
{
if(score[left][right][exLen]) return score[left][right][exLen];
int i, ans, ans2;
ans = segment[right].len + exLen;
ans = ans * ans;
if(left == right) return score[left][right][exLen] = ans;
ans += clickBox(left, right - 1, 0);
for(i = right - 1; i >= left; --i){
if(segment[i].color != segment[right].color) continue;
ans2 = clickBox(left, i, exLen + segment[right].len) +
clickBox(i + 1, right - 1, 0);
if(ans2 > ans) ans = ans2;
}
return score[left][right][exLen] = ans;
} int main()
{
int t, n, i, id, cas = 1;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(i = 0; i < n; ++i) scanf("%d", arr + i);
memset(score, 0, sizeof(score));
segment[id = 0].color = arr[0];
segment[id].len = 1;
for(i = 1; i < n; ++i){
if(arr[i] != arr[i-1]){
segment[++id].color = arr[i];
segment[id].len = 1;
}else ++segment[id].len;
}
printf("Case %d: %d\n", cas++, clickBox(0, id, 0));
}
return 0;
}

最新文章

  1. 浅谈ASM中的SLB
  2. 第十六篇:SWindow的布局属性pos2type及offset
  3. Google Developing for Android 三 - Performance最佳实践
  4. [java基础]分支结构(2)
  5. Python入门笔记(20):Python函数(3):关于lambda
  6. (转载)CentOS6下 源代码方式安装openERP7.0
  7. Flex4 自定义分页组件
  8. 渗透测试实例Windows XP SP2
  9. texCUBE() to CubemapSampler.Sample()
  10. 单点登录CAS使用记(六):单点登出、单点注销
  11. 浅谈JSP(二)
  12. linux 发送带附件的邮件
  13. 几种加密算法的java实现包括MD5、RSA、SHA256
  14. BZOJ 3997: [TJOI2015]组合数学 [偏序关系 DP]
  15. androidpn-server笔记及BUG修改
  16. IIS版本号可以被识别 解决方案
  17. 20181014xlVBA获取小题零分名单
  18. MySql循环语句
  19. linux命令(35):diff命令
  20. 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.2 activation-group&amp; dialect&amp; date-effective

热门文章

  1. String不可变性
  2. B - Beautiful Year
  3. 使用IDEA搭建一个 Spring + Spring MVC 的Web项目(零配置文件)
  4. WordPress瀑布流主题PinThis中文版v1.6.8
  5. Tomcat 报错 记录
  6. nutz 自定义sql的使用
  7. [POI2005]AUT-The Bus 树状数组维护最大前缀和
  8. FFmpeg avcodec_send_packet压缩包函数
  9. html页面之间传值问题
  10. vue 配置页面动态的 title