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. mysql5.7下面windows平台大小写敏感

    转自:https://blog.csdn.net/lrl219/article/details/52889582 根据网上的信息在my.ini下面的mysqld的配置下面添加lower_case_ta ...

  2. 写个js动态调整图片宽高 (原创)

    <body style="TEXT-ALIGN: center;"> <div id="testID" style="backgro ...

  3. Hyper和Vmware冲突,Device/Credential Guard 不兼容

    切换到VM的时候,采用关闭策略 1.PS管理员关闭命令 bcdedit /set hypervisorlaunchtype off 2.系统设置,启用或关闭Windows功能那里,关闭Hyper-V ...

  4. 对JVM还有什么不懂的?一文章带你深入浅出JVM!

    本文跟大家聊聊JVM的内部结构,从组件中的多线程处理,JVM系统线程,局部变量数组等方面进行解析 JVM JVM = 类加载器(classloader) + 执行引擎(execution engine ...

  5. Xcode7.3 使用NSURLSession发送HTTP请求报错

    控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it i ...

  6. MarkDownPad 注册码

    邮箱: Soar360@live.com 授权秘钥: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2M5SN6b ...

  7. 9.19[XJOI] NOIP训练37

    上午[XJOI] NOIP训练37 T1 同余方程 Problem description 已知一个整数a,素数p,求解 $x^{2}\equiv a(mod p) $ 是否有整数解 Solution ...

  8. ASCII和ASCII扩展表

  9. ajax获取跨域数据

    1.效果图 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %&g ...

  10. 豆瓣项目(用react+webpack)

    用豆瓣电影api的项目 电影列表组件渲染 步骤: 1. 发送Ajax请求 1.1 在组件的componentWillMount这个生命周期钩子中发送请求 1.2 发送ajax XMLHttpReque ...