CF 1107 E. Vasya and Binary String
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的更多相关文章
- CF - 1107 E Vasya and Binary String DP
题目传送门 题解: dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值. solve( l, r, k) 代表的是处理 ...
- Codeforces 1107 E - Vasya and Binary String
E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...
- Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...
- [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 ...
- CF1107E Vasya and Binary String
比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着 ...
- Vasya and Binary String(来自codeforces
题目大意: 给定一个0/1字符串,每次你可以将此字符串中一段连续的任意长度的0/1子串消除掉,注意每次消除的子串中只能有0或者1一种字符,消除掉一串长度为i的0/1字符串会得到a[i]的收益,问将这个 ...
- Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)
题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...
- Codeforces1107E. Vasya and Binary String
题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...
- 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 ...
随机推荐
- Java中简单提示异常代码的行号,类名等
public class Test1 { public static void main(String args[]) { System.out.println(getLineInfo()); get ...
- Oracle使用order by排序关于null值处理
select * from dual order by age desc nulls last select * from test order by age asc nulls first sqls ...
- 用tableView实现的一种加载数据的布局
用tableView实现的一种加载数据的布局 此博文是应朋友之邀解决他的业务逻辑问题 效果: 素材: 源码: ImageCell.h 与 ImageCell.m // // ImageCell.h / ...
- UIImageView的animationImages动画
UIImageView的animationImages动画 UIImageView的animationImages,只有在做非常规动画的时候才有优势,比方说下图中左侧动画.如果用来做下图中的右侧动画, ...
- C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数
各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 输出: 解释: 各位相加的过程为: + = , + = . 由于 是一位数,所以返回 . 进阶:你可以 ...
- php面试题之一——php核心技术
一.PHP核心技术 1.写出一个能创建多级目录的PHP函数(新浪网技术部) <?php /** * 创建多级目录 * @param $path string 要创建的目录 * @param $m ...
- Promise & Deferred Objects in JavaScript Pt.2: in Practice
原文:http://blog.mediumequalsmessage.com/promise-deferred-objects-in-javascript-pt2-practical-use Intr ...
- c# 匿名函数与托付
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/han_yankun2009/article/details/26290779 在 2.0之前的 ...
- REST接口设计规范总结
简介 Representational State Transfer 简称 REST 描述了一个架构样式的网络系统.REST 指的是一组架构约束条件和原则.满足这些约束条件和原则的应用程序或设计就是 ...
- 【转】python中的对象拷贝
转自:https://www.cnblogs.com/bhlsheji/p/5352330.html python中.进行函数參数传递或者返回值时,假设是一般的变量,会拷贝传递.假设是列表或字典则是引 ...