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;
}

POJ1390 Blocks 【动态规划】的更多相关文章

  1. POJ1390 Blocks (区间DP)

    题目链接:POJ 1390.Blocks 题意: 有n个方块排成一列,每个方块有颜色即1到n的一个值,每次操作可以把一段相同颜色的方块拿走,长度为k,则获得的分数为 \(k\times k\),求可获 ...

  2. UVA10559&POJ1390 Blocks 区间DP

    题目传送门:http://poj.org/problem?id=1390 题意:给出一个长为$N$的串,可以每次消除颜色相同的一段并获得其长度平方的分数,求最大分数.数据组数$\leq 15$,$N ...

  3. [poj1390]Blocks(方块消除)

    题目大意:给定一序列,可点击某一位置消除与其相邻且相同的方块,得分为$len*len$,求最大得分. 解题关键:关键是状态的构造,首先离散化一下,令$dp[i][j][k]$表示序列$i-j$且后面有 ...

  4. 常规DP专题练习

    POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...

  5. POJ-1390-Blocks (复杂区间DP)

    $ POJ~1390~~Blocks: $ (很难想的区间DP) $ solution: $ 很好的一道题目.看起来似乎很简单,当时一直认为可以用二维区间DP来完成,转移 $ n^3 $ . 后来发现 ...

  6. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  7. 【poj1390】 Blocks

    http://poj.org/problem?id=1390 (题目链接) 题意 给出一排方块,每次可以把颜色相同的消掉,获得长度的平方的分数,问最大得分. Solution 蜜汁dp.. 我们把颜色 ...

  8. poj 1390 Blocks

    poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...

  9. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

随机推荐

  1. [Pulgin] 前端上传组件Plupload使用指南

    我之前写过一篇文章<文件上传利器SWFUpload使用指南>,里面介绍了上传组件SWFUpload的使用方法,但现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SW ...

  2. mysql视图,索引和存储过程

    一:视图 视图又叫虚表.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成. 具体 ...

  3. A - George and Accommodation

    Problem description George has recently entered the BSUCP (Berland State University for Cool Program ...

  4. CentOS6.5下编译R源码并安装Spark R

    不多说,直接上干货! 为了使用SparkR,决定要在Spark所在的Linux上装上R,结果血泪篇了.主要原因是公司内部的虚机,无法连外网,所以网上很多的直接rpm或者yum的方法都没用,需要自己编译 ...

  5. js编写时间选择框

    效果图: 代码: 新建js:WdatePicker.js /* * My97 DatePicker 4.72 Release * License: http://www.my97.net/dp/lic ...

  6. 登录linux,输入ls显示anaconda-ks.cfg cobbler.ks ....., 原因在于root@ ~ / 区别

    今天登录linux测试机,想要创建目录,ls的时候,找不到之前的的目录,才发现是目录不对的问题. 首先,先要弄清楚 [root@330c353813ea ~] 和 [root@330c353813ea ...

  7. Android一对多蓝牙连接示例APP

    一对多蓝牙连接示例,基于Google BluetoothChat修改,实现一对多聊天(一个服务端.多个客户端),类似聊天室. 主要功能: 客户端的发出的消息所有终端都能收到(由服务端转发) 客户端之间 ...

  8. C# 学习笔记_类

    定义:将成员及方法封装到类中,类的实例则称为对象. 结构:属性,类修饰符,class,类名,{类体} 类修饰符:new,public,protected,internal,private,abstra ...

  9. 使用Word 2010群发邮件

    1.建立数据库,这里我使用了excel 字段:电子邮件地址,名字 填写需要发送的数据 2.新建word文档,这里我使用了word2010 点击工具栏邮件 开始邮件合并,电子邮件 选择收件人,使用现有列 ...

  10. winserver2012安装.net 3.5

    运行 dism.exe /online /enable-feature /featurename:NetFX3 /Source:I:\sources\sxs