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 ...
随机推荐
- 【做题】CF196E. Opening Portals 排除无用边&最小生成树
题意:给出一个有\(n\)个结点,\(m\)条边的连通无向图,边有边权,等于经过这条边所需的时间.有\(k\)个点设有传送门.一开始,所有传送门关闭.你从\(1\)号点出发,每当你到达一个有传送门的点 ...
- SSM到Spring Boot从零开发校园商铺平台
项目目的 特别 由于准备春招,所以希望各位看客方便的话,能去github上面帮我Star一下项目 https://github.com/Draymonders/Campus-Shop emmm, 已经 ...
- (zhuan) Recurrent Neural Network
Recurrent Neural Network 2016年07月01日 Deep learning Deep learning 字数:24235 this blog from: http:/ ...
- 用.native修饰器来对外部组件进行构造器内部方法的调用以及用原生js获取构造器里的方法
html <div id="app"> <span v-text="number"></span> <btn @cli ...
- 在 JSDOM v11 中使用jQuery
在JSDOM v11中使用jQuery 从v10开始,jsdom提供了更灵活的API. https://swashata.me/blog/use-jquery-jsdom-v11/ const tes ...
- angularjs启动项目报ERROR in AppModule is not an NgModule解决方法
这主要是ts编译器版本问题,一般是因为ts编译器版本过高导致. 解决方式: npm uninstall -g typescript npm install -g typescript tsc -v 查 ...
- Python学习 day04打卡
今天学习的主要内容: 一,列表 1,列表的介绍 列表是python的基础数据类型之一,其他编程语音也有类似的数据类型.例如:JS 中的数组Java中的数组等等. 它是以[]括起来,每个元素用',隔开而 ...
- 转一篇 ShaderVariantCollection介绍的比较详细的文章 感谢作者
http://www.seven-fire.cn/archives/174 Unity3D Shader加载时机和预编译 焱燚(七火) | 2016年7月6日 | UnityShader ...
- hdu 3208 Integer’s Power 筛法
Integer’s Power Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 跳跳虎回家(国庆10.1模拟赛T2)
题目: [题目描述] 跳跳虎在外面出去玩忘了时间,现在他需要在最短的时间内赶回家. 跳跳虎所在的世界可以抽象成一个含有 n 个点的图(点编号从 1 到 n ),跳跳虎现在在 1 号点,跳跳虎的家在 n ...