E. Vasya and Binary String

链接

分析:

  对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可。

  然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为一个点。设f[i][j][k]表示消完区间[i,j]和这段区间后面k个元素最大值,其中k个元素的颜色与点j的颜色相同。

  转移:可以首先将j和后面k个元素消除,然后消除[i,j-1]。也可以枚举一个和j颜色相同的点m,然后分别先消除[m+1,r-1],剩下的区间就和后面k个连在一起了,再求出这段区间的答案。

  复杂度$O(n^4)$,但是跑不满,再记忆化一下,31ms。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
int n, cnt;
LL a[N], f[N], g[N][N][N];
char s[N];
struct Node { int size, col; } b[N]; void init() {
for (int i = ; i <= n; ++i)
for (int j = i; j <= n; ++j) f[j] = max(f[j], f[j - i] + a[i]);
int now = ;
for (int i = ; i <= n; ++i) {
if (s[i] == s[i - ]) now ++;
else cnt ++, b[cnt].size = now, b[cnt].col = s[i - ] - '', now = ;
}
++cnt; b[cnt].size = now, b[cnt].col = s[n] - '';
}
LL DP(int l,int r,int k) {
if (l == r) return f[b[l].size + k];
if (~g[l][r][k]) return g[l][r][k];
LL res = DP(l, r - , ) + f[b[r].size + k];
for (int i = l; i < r - ; ++i)
if (b[i].col == b[r].col) res = max(res, DP(i + , r - , ) + DP(l, i, b[r].size + k));
return g[l][r][k] = res;
}
int main() {
n = read();
scanf("%s", s + );
for (int i = ; i <= n; ++i) a[i] = read();
init();
memset(g, -, sizeof(g));
cout << DP(, cnt, );
return ;
}

CF 1107 E. Vasya and Binary String的更多相关文章

  1. CF - 1107 E Vasya and Binary String DP

    题目传送门 题解: dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值. solve( l, r, k) 代表的是处理 ...

  2. Codeforces 1107 E - Vasya and Binary String

    E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...

  3. Codeforces1107E Vasya and Binary String 记忆化dp

    Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...

  4. [CF1107E]Vasya and Binary String【区间DP】

    题目描述 Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of l ...

  5. CF1107E Vasya and Binary String

    比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着 ...

  6. Vasya and Binary String(来自codeforces

    题目大意: 给定一个0/1字符串,每次你可以将此字符串中一段连续的任意长度的0/1子串消除掉,注意每次消除的子串中只能有0或者1一种字符,消除掉一串长度为i的0/1字符串会得到a[i]的收益,问将这个 ...

  7. Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)

    题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...

  8. Codeforces1107E. Vasya and Binary String

    题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...

  9. CF 1003B Binary String Constructing 【构造/找规律/分类讨论】

    You are given three integers a, b and x. Your task is to construct a binary string s of length n=a+b ...

随机推荐

  1. JAVA使用POI如何导出百万级别数据

    用过POI的人都知道,在POI以前的版本中并不支持大数据量的处理,如果数据量过多还会常报OOM错误,这时候调整JVM的配置参数也不是一个好对策(注:jdk在32位系统中支持的内存不能超过2个G,而在6 ...

  2. Flask 参数简介

    我们都知道学习了Flask的时候它里面的参数是有很多种的参数  都是需要相互进行调用传递的  今天就简要分析一些常见的参数 首先导入Flask之后看 源码 from flask import Flas ...

  3. [翻译] BAFluidView

    BAFluidView https://github.com/antiguab/BAFluidView This view and it's layer create a 2D fluid anima ...

  4. [UI] 精美UI界面欣赏[3]

    精美UI界面欣赏[3]

  5. 山寨今日头条的标题title效果

    山寨今日头条的标题title效果 效果: 源码: // // ViewController.m // 今日头条 // // Created by YouXianMing on 14/11/26. // ...

  6. swift中,Optional、?与!之间的关系

    swift中,Optional.?与!之间的关系 Optional <ClassName> 与 ClassName? 等价 对 ClassName! 强制取值会导致崩溃(如果对象为nil时 ...

  7. September 23rd 2017 Week 38th Saturday

    Lonely people will always remember his life occurred in each person. 寂寞的人总是会用心记住他生命中出现过的每个人. If you ...

  8. gamit安装

    需要准备的文件: 默认已安装好虚拟机和Ubuntu系统 1.输入用户名密码,进入Ubuntu10.04桌面.按下“Ctrl+Alt+T”,进入终端: 2.在终端输入“sudo gedit /etc/a ...

  9. 题解 P1868 【饥饿的奶牛】

    题目链接:P1868 饥饿的奶牛 题面 有一条奶牛冲出了围栏,来到了一处圣地(对于奶牛来说),上面用牛语写着一段文字. 现用汉语翻译为: 有N个区间,每个区间x,y表示提供的x~y共y-x+1堆优质牧 ...

  10. echo,die(),print(),print_r(),var_dump()的区别

    echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)  print()   只能打印出简单类型变量的值(如int,string)  例子 输出 3 ...