D1. Kirk and a Binary String (easy version)

01串找最长不降子序列

给定字符串s,要求生成一个等长字符串t,使得任意l到r位置的最长不降子序列长度一致

从后往前暴力枚举,枚举每个一替换成0后是否改变了l到r位置的最长不降子序列长度

01串的最长不降子序列,可以通过线性dp求解

dp i表示以i结尾的最长不降子序列长度

dp[0]=dp[0]+s[i]=='0';

dp[1]=max(dp[0],dp[1])+s[i]=='1';

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sc(x) scanf("%I64d",&(x));
typedef long long ll;
#define maxn 2005 #define INF 1e18
ll N;
ll val[][maxn];
ll dp[];
void LIS(string s,int st,int val[])
{
dp[]=dp[]=;
for(int i=st;i<N;i++){
if(s[i]==''){
dp[]++;
}else dp[]=max(dp[],dp[])+;
val[i]=max(dp[],dp[]);
} }
signed main()
{
string s,t;
cin>>s;
N=s.size();
t=s;
//int len=0;
for(int i=N-; i>=;i--)
{
if(s[i]=='')
{
t[i]='';
LIS(s,i,val[]);
LIS(t,i,val[]);
for(int j=i;j<N;j++){
if(val[][j]!=val[][j]){
t[i]='';
break;
}
}
}
}
cout<<t<<'\n';
}

下面这个大概思路是从后往前枚举,后面的0个数比1个数小时,可以删当前位置1

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sc(x) scanf("%I64d",&(x));
typedef long long ll;
#define maxn 2005 #define INF 1e18
ll N;
ll val[][maxn];
ll dp[];
void LIS(string s,int st,int val[])
{
dp[]=dp[]=;
for(int i=st;i<N;i++){
if(s[i]==''){
dp[]++;
}else dp[]=max(dp[],dp[])+;
val[i]=max(dp[],dp[]);
} }
signed main()
{
string s,t;
cin>>s;
N=s.size();
t=s;
int cnt=;
for(int i=N-; i>=;i--)
{
if(s[i]==''){
cnt++;
}else if(cnt==&&s[i]==''){
t[i]='';
}else cnt--;
}
cout<<t<<'\n'; }

D1. Kirk and a Binary String (easy version)的更多相关文章

  1. D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)

    D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...

  2. Codeforces 1204D2. Kirk and a Binary String (hard version) (dp思路)

    题目链接:http://codeforces.com/contest/1204/problem/D2 题目是给定一个01字符串,让你尽可能多地改变1变为0,但是要保证新的字符串,对任意的L,R使得Sl ...

  3. 01串LIS(固定串思维)--Kirk and a Binary String (hard version)---Codeforces Round #581 (Div. 2)

    题意:https://codeforc.es/problemset/problem/1204/D2 给你一个01串,如:0111001100111011101000,让你改这个串(使0尽可能多,任意 ...

  4. CF1204D Kirk and a Binary String

    题目链接 problem 给出一个长度为\(n(n\le 10^5)\)的只包含01的字符串.把尽可能多的1变为0,使得对于所有的\(l \in [1,n],r\in [l,n]\),区间\([l,r ...

  5. Codeforces 1204D Kirk and a Binary String - 数学

    题目传送门 传送门 群除我均会猜结论/找规律,sad.... 以下内容只保证代码能过system test,证明应该都是在纯口胡 约定下文中的$LIS$表示最长不下降子序列. 定义$zero(s)$表 ...

  6. CF #579 (Div. 3) D1.Remove the Substring (easy version)

    D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...

  7. Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)

    D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...

  8. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  9. E1. String Coloring (easy version)(贪心)

    E1. String Coloring (easy version) time limit per test 1 second memory limit per test 256 megabytes ...

随机推荐

  1. MySQL-快速入门(2)数据类型

    1.MySQL支持多种数据类型: 1>数值类型:整数类型tinyint.smallint.mediumint.bigint.int:浮点小数类型float.double:定点小数类型decima ...

  2. 一些DP上的奇奇怪怪的东西

    单调队列&单调栈: 有手就行.jpg 四边形不等式: 若\(w(i,j)\)满足\(\forall a\le b<c\le d,w(a,c)+w(b,d)\le w(b,c)+w(a,d ...

  3. 题解 CF1140D 【Minimum Triangulation】

    题意:求将一个n边形分解成(n-2)个三边形花费的最小精力,其中花费的精力是所有三角形的三顶点编号乘积的和(其中编号是按照顶点的顺时针顺序编写的) 考虑1,x,y连了一个三角形,x,y,z连了一个三角 ...

  4. while循环和字符串格式化

    小知识点 \n#换行 \t #制表 \r #回车 print(a,b,c,d,sep="\n")换行 sep默认空格 1.while--关键字(死循环) while 空格 条件: ...

  5. HNUSTOJ-1257 You are my brother

    1257: You are my brother 时间限制: 1 Sec  内存限制: 128 MB提交: 39  解决: 15[提交][状态][讨论版] 题目描述 Little A gets to ...

  6. linux 环境 Xshell操作数据库

    一:采用sqlplus连接登录(确保安装了sqlplus) 1:先登陆进入到oracle的数据库的服务器环境下 2:切换到sqlplus操作:  sqlplus /nolog 3:conn /as s ...

  7. vscode配置汇总

    一.ESlint插件的作用:格式化代码 二.vetur插件:

  8. C/C++ 递归

    递归 当一个函数调用它自己来定义时称它为递归函数.(什么叫它自己调用它自己呢?) 1.1.引出递归 从一个简单的问题考虑递归,求0,1,2, 3,4,5......n的和. 首先定义一个求和公式:su ...

  9. CentOS7.6中 KVM虚拟机内存、CPU调整

    CentOS7.6中 KVM虚拟机内存.CPU调整 一.调小虚拟机内存 调小虚拟机内存可以动态实现,不用关机 1.查看当前内存大小 [root@heyong kvm]# virsh dominfo t ...

  10. Apache 配置IP站点

    配置临时生效 IP: [root@Nagios-Server extra]# ifconfigeth0:0 192.168.1.126/24 up [root@Nagios-Server extra] ...