CF1107E Vasya and Binary String
比赛的时候又被垃圾题艹翻了啊。
这个题显然是区间dp
考虑怎么转移。
类似消除方块和ZYB玩字符串那样的一个DP。
可以从左到右依次考虑消除。
dp[l][r][k][flag]表示区间l,r左边粘着k个flag。
转移方式:
1.考虑强行去继续黏上下一个字符并使k+1。
2.考虑把一段跳过去,让被跳过的这一段自行不与外界产生关系被消除,然后再去消除后面的。
3.直接消除当前粘着的一段,并获得收益。
#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define N 110
#define L 100
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline ll read()
{
char ch=0;
ll x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
char s[N];
ll a[N],f[N],dp[N][N][N][2];
ll dfs(ll l,ll r,ll k,ll flag)
{
if(l>r)return a[k];
if(dp[l][r][k][flag]!=-1)return dp[l][r][k][flag];
if(!k||(k&&f[l]==flag))
dp[l][r][k][flag]=max(dp[l][r][k][flag],dfs(l+1,r,k+1,f[l]));
for(ll i=l;i<=r;i++)
dp[l][r][k][flag]=max(dp[l][r][k][flag],dfs(l,i,0,0)+dfs(i+1,r,k,flag));
for(ll i=1;i<=k;i++)
dp[l][r][k][flag]=max(dp[l][r][k][flag],dfs(l,r,k-i,flag)+a[i]);
return dp[l][r][k][flag];
}
int main()
{
ll n=read();
scanf("%s",s+1);
for(ll i=1;i<=n;i++)a[i]=read(),f[i]=s[i]-'0';
memset(dp,-1,sizeof(dp));
printf("%lld",dfs(1,n,0,0));
return 0;
}
CF1107E Vasya and Binary String的更多相关文章
- [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 ...
- Codeforces 1107 E - Vasya and Binary String
E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...
- CF 1107 E. Vasya and Binary String
E. Vasya and Binary String 链接 分析: 对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可. 然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为 ...
- Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...
- 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,和此题很 ...
- CF - 1107 E Vasya and Binary String DP
题目传送门 题解: dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值. solve( l, r, k) 代表的是处理 ...
- Codeforces1107E. Vasya and Binary String
题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
随机推荐
- Ubuntu 18.04 休眠后无法唤醒的解决办法
解决办法:安装laptop-mode-tools工具包. 1.检查是否安装了grep laptop-mode-tools 工具包 $ dpkg -l | grep laptop-mode-tools ...
- Windows操作系统下安装Ubuntu虚拟机
认识VMware虚拟机 VMware(虚拟机)是指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统,它能在Windows系统上虚拟出多个计算机,每个虚拟计算机可以独立运行 ...
- Calculate difference between consecutive data points in a column from a file
cat > temp0015101269125 awk 'p{print $0-p}{p=$0}' temp00152-633-7 REF: https://www.unix.com/shel ...
- SP3946 MKTHNUM - K-th Number(整体二分)
思路 整体二分的板子题,没什么思路好说 代码 #include <cstdio> #include <algorithm> #include <cstring> u ...
- eclipse创建springBoot项目
创建Spring Boot 工程 先在eclipse中安装spring -tool -suite插件,然后根据以下步骤可以创建1.新建Spring Starter Project 2.Packagin ...
- vs里32位项目和64位项目的区别
由于操作系统内存分配的不同,导致软件开发过程中,需要编译不同版本的软件. 1.编译程序根据需要选择不同的编译环境. x86和win32为32位程序,x64为64位程序,可以选择不同的编译条件形成不同位 ...
- json扩展
using Newtonsoft.Json.Linq; namespace Utility { public static class JsonExt { /// <summary> // ...
- ERR! registry error parsing json
报错日志: ERR! registry error parsing json ERR! registry error parsing json 解决过程: 从github上克隆一个项目,在npm i的 ...
- Oracle 11G Client客户端安装
参考资料: http://www.cnblogs.com/jiguixin/archive/2011/09/09/2172672.html http://blog.csdn.net/lanchengx ...
- 安装ik分词器以及版本和ES版本的兼容性
一.查看自己ES的版本号与之对应的IK分词器版本 https://github.com/medcl/elasticsearch-analysis-ik/blob/master/README.md 二. ...