<更新提示>

<第一次更新>


<正文>

Blocks

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:

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 kk points. for example, if you click on a silver box, the silver segment disappears, you got 44=16 points.

Now let's look at the picture below:

The first one is OPTIMAL.

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

题意:通过点击某一颜色消除相邻的所有的这种颜色,得分为len*len,求最大分

Input Format

第一行为一个整数 N。

第二行为 A1,A2,…,AN。

Output Format

一行一个整数,代表最大价值。

Sample Input

9
1 2 2 2 2 3 3 3 1

Sample Output

29

解析

这种区间操作类的最优解问题显然是区间\(dp\),不过这道题的状态有点棘手。

对于一次消除操作,可能会带来左右两边原本不相邻的部分合并带来的影响,所以通常来说的状态是不行了。我们考虑设计一种状态能够记录这种合并带来的影响:设\(f[l][r][k]\)代表区间消除\([l,r]\),序列后面通过消除操作使得有\(k\)个颜色为\(a[r]\)的块跟在区间\([l,r]\)后面的最大得分。

考虑转移,显然,我们可以直接将后面跟着的\(k\)个块和第\(r\)个块连在一起消掉,这是一种转移方式。还有就是我们可以在区间\([l,r-1]\)中枚举一个颜色与\(a[r]\)相同的点\(i\),然后将区间\([i+1,r-1]\)作为一个子问题直接消去,这样就使得\(a[r]\)加入后面跟着的\(k\)个块中,\(i\)成为右端点,利用这个状态转移即可。

至于如何枚举区间内和\(a[r]\)颜色相同的点呢?这是可以预处理直接向前查找的。

\(Code:\)

#include<bits/stdc++.h>
using namespace std;
const int N = 220;
int n,a[N],pre[N],last[N];
int f[N][N][N];
inline void input(void)
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
}
inline void init(void)
{
for (int i=1;i<=n;i++)
{
pre[i] = last[a[i]];
last[a[i]] = i;
}
}
inline int dp(int l,int r,int k)
{
if ( l > r ) return 0;
if ( f[l][r][k] ) return f[l][r][k];
f[l][r][k] = dp( l , r-1 , 0 ) + (k+1) * (k+1);
for (int i=pre[r];i>=l;i=pre[i])
f[l][r][k] = max( f[l][r][k] , dp( l , i , k+1 ) + dp( i+1 , r-1 , 0 ) );
return f[l][r][k];
}
int main(void)
{
input();
init();
memset( f , 0 , sizeof f );
dp( 1 , n , 0 );
printf("%d\n",f[1][n][0]);
return 0;
}

<后记>

『Blocks 区间dp』的更多相关文章

  1. 『count 区间dp』

    count Description 既然是萌萌哒 visit_world 的比赛,那必然会有一道计数题啦! 考虑一个N个节点的二叉树,它的节点被标上了1-N的编号. 并且,编号为i的节点在二叉树的前序 ...

  2. 『金字塔 区间dp』

    金字塔 Description 虽然探索金字塔是极其老套的剧情,但是这一队 探险家还是到了某金字塔脚下.经过多年的研究,科 学家对这座金字塔的内部结构已经有所了解.首先, 金字塔由若干房间组成,房间之 ...

  3. 【POJ-1390】Blocks 区间DP

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

  4. POJ 1390 Blocks(区间DP)

    Blocks [题目链接]Blocks [题目类型]区间DP &题意: 给定n个不同颜色的盒子,连续的相同颜色的k个盒子可以拿走,权值为k*k,求把所有盒子拿完的最大权值 &题解: 这 ...

  5. POJ1390 Blocks (区间DP)

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

  6. UVA10559&POJ1390 Blocks 区间DP

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

  7. UVA 10559 Blocks——区间dp

    题目:https://www.luogu.org/problemnew/show/UVA10559 应该想到区间dp.但怎么设计状态? 因为连续的东西有分值,所以应该记录一下连续的有多少个. 只要记录 ...

  8. UVA 10559 Blocks —— 区间DP

    题目:https://www.luogu.org/problemnew/show/UVA10559 区间DP,有点难想: 为了方便,先把原来就是连续一段相同颜色的点看做一个点,记一下长度: f[i][ ...

  9. POJ 1390 Blocks (区间DP) 题解

    题意 t组数据,每组数据有n个方块,给出它们的颜色,每次消去的得分为相同颜色块个数的平方(要求连续),求最大得分. 首先看到这题我们发现我们要把大块尽可能放在一起才会有最大收益,我们要将相同颜色块合在 ...

随机推荐

  1. unity 2d碰撞/ui组件碰撞

    首先,ugui的碰撞是可以用Collision2D跟Rigidbody2D实现的(就跟3D碰撞一样).之前试过不可以主要问题正在于Collision2D以及Rigidbody的设置上. 碰撞双方都添加 ...

  2. ORACLE百分比分析函数RATIO_TO_REPORT() OVER()

    有时候不用的指标的绝对值不能比,但是转转为百分比的形式就容易看出波动了,是数据分析的好用的一个分析函数 20:00:24 SYS@orcl> conn scott/tiger; Connecte ...

  3. windows系统DOC命令启动或停止服务

    -- 启动服务 -- net start Mysql -- 停止服务 -- net stop Mysql-- 说明:MysqL没有大小写区分.可以直接小写mysql. -- 启动报错(net star ...

  4. libass简明教程

    [时间:2019-05] [状态:Open] [关键词:字幕,libass,字幕渲染,ffmpeg, subtitles, video filter] 0 引言 libass库则是一个轻量级的对ASS ...

  5. 前端性能优化 css和js的加载与执行

    一个网站在浏览器端是如何进行渲染的? html本身首先会被渲染成 DOM 树,实际上 html 是最先通过网址请求过来的,请求过来之后,html 本身会由一个字节流转化成一个字符流,浏览器端拿的就是字 ...

  6. 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)

    目录 题目链接 思路 代码 题目链接 传送门 思路 首先我们将原式化简: \[ \begin{aligned} &\sum\limits_{l_1=1}^{n}\sum\limits_{l_2 ...

  7. robotframework连接mysql

    1.安装mysql数据库,并启动,创建数据库test及user表(可以自定义) 2.配置robotframework环境加载DatabaseLibrary 3.安装pymysql,下载地址:https ...

  8. 解决tomcat出现乱码问题---韦大仙

    1. 改这两个文件 URIEncoding="UTF-8" 2.然后重启idea

  9. python 通过scapy获取网卡列表

    python通过scapy 获取网卡列表如下: #coding:utf-8 from scapy.all import * #显示网卡信息 show_interfaces() 运行结果如下:

  10. Checking Types Against the Real World in TypeScript

    转自:https://www.olioapps.com/blog/checking-types-real-world-typescript/ This is a follow-up to Type-D ...