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]\)的最长不下降子序列的长度不变。
solution
【译自官方题解】
可以发现有些字符是确定的(即无法修改)。这些确定的字符满足以下几个条件。
- 所有的\(10\)是确定的。
- 如果字符串\(p\)是确定的且字符串\(q\)是确定的,那么字符串\(pq\)是确定的。
- 如果字符串\(p\)是确定的,那么字符串\(1p0\)是确定的。
- 根据以上三个性质可以推出,所有确定的字符串中0和1的个数相同
- 确定的字符串的最长不降子序列长度为该字符串长度的一半。具体的就是取全部的1或全部的0。
【下方内容为自己YY】
然后考虑如何具体实现。发现上面的5条性质(其实是前3条)与括号序列相同。所以实现就非常简单了。将1看做左括号,0看做右括号。最后将不能匹配的左括号全部变为0即可。
code
/*
* @Author: wxyww
* @Date: 2019-08-21 15:00:10
* @Last Modified time: 2019-08-21 15:01:55
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
#include<cmath>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N = 100000 + 100;
ll read() {
ll x = 0,f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0',c = getchar();}
return x * f;
}
char s[N];
int a[N],top;
int main() {
scanf("%s",s + 1);
int n = strlen(s + 1);
for(int i = 1;i <= n;++i) {
if(s[i] == '1') a[++top] = i;
else if(top) --top;
}
while(top) s[a[top--]] = '0';
printf("%s",s + 1);
return 0;
}
CF1204D Kirk and a Binary String的更多相关文章
- 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 ...
- D1. Kirk and a Binary String (easy version)
D1. Kirk and a Binary String (easy version) 01串找最长不降子序列 给定字符串s,要求生成一个等长字符串t,使得任意l到r位置的最长不降子序列长度一致 从后 ...
- Codeforces 1204D Kirk and a Binary String - 数学
题目传送门 传送门 群除我均会猜结论/找规律,sad.... 以下内容只保证代码能过system test,证明应该都是在纯口胡 约定下文中的$LIS$表示最长不下降子序列. 定义$zero(s)$表 ...
- 01串LIS(固定串思维)--Kirk and a Binary String (hard version)---Codeforces Round #581 (Div. 2)
题意:https://codeforc.es/problemset/problem/1204/D2 给你一个01串,如:0111001100111011101000,让你改这个串(使0尽可能多,任意 ...
- Codeforces 1204D2. Kirk and a Binary String (hard version) (dp思路)
题目链接:http://codeforces.com/contest/1204/problem/D2 题目是给定一个01字符串,让你尽可能多地改变1变为0,但是要保证新的字符串,对任意的L,R使得Sl ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
随机推荐
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题
A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...
- .NET 时间轴:从出生到巨人
自1995年互联网战略日以来最雄心勃勃的事业 —— 微软.NET战略, 2000年6月30日. 微软于2000年推出基于Windows操作系统的应用软件开发框架.NET,发展至今形成巨大的技术栈,涉及 ...
- 《细说PHP》第四版 样章 第二章 PHP的应用与发展 2
2.2 PHP的应用 任何一种主流的编程语言,几乎都可以开发任何类型的软件.编程语言就是一种开发工具,而选择适合的工具去做适合的事儿,才能体现其应用价值.PHP最主要的应用,就是与数据库交互来开发W ...
- 在Unity中使用自定义宏
最近写AVG工具时有这样的功能需求,AVG的角色可以支持动态的Spine动画,当没有Spine动画时采用默认的立绘图片替代. 这时在脚本中就可以采用自定义的宏来实现: 例如: #if VNSpine ...
- Java报错:java.math.BigDecimal cannot be cast to java.lang.String
从数据库取数字,转为string,报错: java.math.BigDecimal cannot be cast to java.lang.String 错误代码 Integer.parseInt(( ...
- 第2个word发布的博客
//接收的为空时,则表示客户端下线,跳出循环 if (r == 0) { break; }; string str = Encoding.UTF8.GetString(buffer, 0, r); ...
- Set a One-to-Many Relationship设置一对多关系 (XPO)
In this lesson, you will learn how to set a one-to-many relationship between business objects. The C ...
- MySQL数据库的事务及存储引擎
一.关系型数据库与非关系型数据库 1.关系型数据库的特点: 1)数据以表格的形式出现 2)每行为各种记录名称 3)每列为记录名称所对应的数据域 4)许多的行和列组成一张表单 5)若干的表单组成数据库 ...
- jsp表单数据添加到数据库
先由表单提交数据到某验证页面:checkregister.jsp<form method="POST"name="form1" action=" ...
- 多线程CGD调度组原理
我们常用的GCD调度组方式 //GCD常用调度组写法 -(void)demo1{ //创建调度组和队列 dispatch_group_t group = dispatch_group_create() ...