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 megabytes
inputstandard input
outputstandard output
The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.
Kirk has a binary string s (a string which consists of zeroes and ones) of length n and he is asking you to find a binary string t of the same length which satisfies the following conditions:
For any l and r (1≤l≤r≤n) the length of the longest non-decreasing subsequence of the substring slsl+1…sr is equal to the length of the longest non-decreasing subsequence of the substring tltl+1…tr;
The number of zeroes in t is the maximum possible.
A non-decreasing subsequence of a string p is a sequence of indices i1,i2,…,ik such that i1<i2<…<ik and pi1≤pi2≤…≤pik. The length of the subsequence is k.
If there are multiple substrings which satisfy the conditions, output any.
Input
The first line contains a binary string of length not more than 105.
Output
Output a binary string which satisfied the above conditions. If there are many such strings, output any of them.
Examples
inputCopy
110
outputCopy
010
inputCopy
010
outputCopy
010
inputCopy
0001111
outputCopy
0000000
inputCopy
0111001100111011101000
outputCopy
0011001100001011101000
Note
In the first example:
For the substrings of the length 1 the length of the longest non-decreasing subsequnce is 1;
For l=1,r=2 the longest non-decreasing subsequnce of the substring s1s2 is 11 and the longest non-decreasing subsequnce of the substring t1t2 is 01;
For l=1,r=3 the longest non-decreasing subsequnce of the substring s1s3 is 11 and the longest non-decreasing subsequnce of the substring t1t3 is 00;
For l=2,r=3 the longest non-decreasing subsequnce of the substring s2s3 is 1 and the longest non-decreasing subsequnce of the substring t2t3 is 1;
The second example is similar to the first one.
题意:
给你一个字符串s,只包含0,1两个字符,
让你寻找一个字符串t,长度和s相等,并且0尽可能的多,
同时满足 对于因为一个区间 l and r (1≤l≤r≤n) 两个字符串的最长不下降子序列长度相等。
思路:
我们首先来看下什么情况下会产生最长不下降子序列长度不相等。
例如区间中:
s:10
t: 00
显然s的长度为1,而t的为2,
即有“10” 的位置才可能产生最长不下降子序列长度不相等。
那么再来看长一点的
s:1100
t: 0100
s 最长不下降子序列长度 为2 (11 或者 00)
t:最长不下降子序列长度为3 (“000”)
想让t的最长不下降子序列长度变小点
我们可以把第一个0换成1,即也为1100
再来看这个:
s:01100
t: 00100
需要用1替换第二个0才满足条件。
而
s:01111110
t: 0000010
就已经满足了条件,
由此我们可以发现结论,
当s和t同时从后向前扫,t中出现的1的个数大于等于s中出现0的个数才满足条件,满足条件的时候,t尽可能多填0.
通过这个结论我们就很容易得出答案。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=100010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string str;
struct node
{
int t;
int num;
}a[maxn];
string ans="";
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
cin>>str;
int len=str.length();
int cnt=0;
for(int i=0;i<len;++i)
{
if(str[i]=='0')
{
int num=1;
while((i+1)<len&&(str[i+1]=='0'))
{
num++;
i++;
}
a[++cnt].t=0;
a[cnt].num=num;
}else
{
int num=1;
while((i+1)<len&&(str[i+1]=='1'))
{
num++;
i++;
}
a[++cnt].t=1;
a[cnt].num=num;
}
}
int need=0;
for(int i=cnt;i>=1;--i)
{
if(a[i].t==0)
{
need+=a[i].num;
repd(j,1,a[i].num)
{
ans.push_back('0');
}
}else
{
int k=min(a[i].num,need);
a[i].num-=k;
need-=k;
while(k--)
{
ans.push_back('1');
}
repd(j,1,a[i].num)
{
ans.push_back('0');
}
}
}
reverse(ALL(ans));
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)的更多相关文章
- 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 Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造
B2. Character Swap (Hard Version) This problem is different from the easy version. In this version U ...
- Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈,递推)
Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 题意: 你是一名建筑工程师,现给出 n 幢建筑的预计建设高度,你想建成峰状, ...
- Codeforces Round #721 (Div. 2)A. And Then There Were K(位运算,二进制) B1. Palindrome Game (easy version)(博弈论)
半个月没看cf 手生了很多(手动大哭) Problem - A - Codeforces 题意 给定数字n, 求出最大数字k, 使得 n & (n−1) & (n−2) & ...
- 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 ...
随机推荐
- 日期格式存入字符类型之后,再转回datetime类型报错
背景 最近我们迁移了一批服务器,因我们在azure portal 上新开的VM暂时默认只有英文系统,所以我们在开设好的数据库服务器的时候,都会重置数据库字符排序成中文,避免出现中文乱码问题,重置参考路 ...
- 基于axis的WebService的案例
1.axis axis是Apache旗下产品,它是一个webservice容器,可被用于创建.部署和运行webservice.网址http://axis.apache.org/,网址上的部分截图如下. ...
- MUT值设置、top等命令无法执行、ssh无法登陆、vim命令卡住
[root@host---- ~]# ifconfig eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu inet 10.1.1.204 n ...
- Data - 【转】数据统计、数据挖掘、大数据、OLAP的区别
原文链接 数据分析 数据分析是一个大的概念,理论上任何对数据进行计算.处理从而得出一些有意义的结论的过程,都叫数据分析. 从数据本身的复杂程度.以及对数据进行处理的复杂度和深度来看,可以把数据分析分为 ...
- python高级 之(四) --- 模块方法
模块 时间模块:time/datatime/calendar. 系统操作文件模块:os time模块 介绍 import time # 获取当前时间, 结果是存在时间元组中的.星期一是从0开始的 # ...
- Java组件介绍
1.String string对象的不变性.被final关键字修饰. 使用charat方法替代startwith/endwith方法(提高性能) 使用buffer(方法同步,性能较差)和builder ...
- make j* make j4 make j8 区别
转载: make -j4是什么意思看书上说1) make(1)只衍生一个作业//作业是什么意思?make(1) 是不是就是make的意思?2) 在双处理器上make -j4,难道是让每个处理器跑两个作 ...
- 设计模式:策略模式(Stratege)
首先我们需要知道策略模式与状态模式是如此的相似,就犹如一对双胞胎一样.只不过状态模式是通过改变对象内部的状态来帮助对象控制自己的行为,而策略模式则是围绕可以互换的算法来创建成功业务的.两者都可用于解决 ...
- Navicat 连接数据库避免中文显示乱码问题解决
在使用Navicat Premium连接数据库进行操作时,为避免出现中文乱码的问题解决: 1.连接SQL Server 在新建数据库时,常规 设置 排序规则 为 Chinese_PRC_CS_AS_W ...
- BugkuCTF--never give up
这道题还挺有意思的... http://123.206.87.240:8006/test/hello.php 查看元素,有个1p.html,访问. 还没看到网页元素就跳转了...抓包! 抓到了一堆东西 ...