Codeforces Round #598 (Div. 3) D. Binary String Minimizing
You are given a binary string of length nn (i. e. a string consisting of nn characters '0' and '1').
In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than kk moves? It is possible that you do not perform any moves at all.
Note that you can swap the same pair of adjacent characters with indices ii and i+1i+1 arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.
You have to answer qq independent test cases.
The first line of the input contains one integer qq (1≤q≤1041≤q≤104) — the number of test cases.
The first line of the test case contains two integers nn and kk (1≤n≤106,1≤k≤n21≤n≤106,1≤k≤n2) — the length of the string and the number of moves you can perform.
The second line of the test case contains one string consisting of nn characters '0' and '1'.
It is guaranteed that the sum of nn over all test cases does not exceed 106106 (∑n≤106∑n≤106).
For each test case, print the answer on it: the lexicographically minimum possible string of length nn you can obtain from the given one if you can perform no more than kk moves.
3
8 5
11011010
7 9
1111100
7 11
1111100
01011110
0101111
0011111
In the first example, you can change the string as follows: 110–––11010→10–––111010→011110–––10→01110–––110→0110–––1110→01011110110_11010→10_111010→011110_10→01110_110→0110_1110→01011110.
In the third example, there are enough operations to make the string sorted.
尽量把0往前移动
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int q;
cin >> q;
while (q--) {
int n;
long long k;
cin >> n >> k;
string s;
cin >> s;
int j = ;
for (int i = ; i < n; i++) {
if (j < i) j = i;
while (j < n && s[j] != '') j++; //判断前面某个0前面有几个1
if (j < n && j - i <= k) {//判断能否直接换过去
swap(s[i], s[j]);
k -= j - i;
}
}
cout << s << endl;
}
return ;
}
Codeforces Round #598 (Div. 3) D. Binary String Minimizing的更多相关文章
- Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心
D. Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #598 (Div. 3)
传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...
- Codeforces Round #598 (Div. 3) A,B,C,D{E,F待补}
A. Payment Without Change #include<bits/stdc++.h> using namespace std; #define int long long ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- Codeforces Round #303 (Div. 2) B. Equidistant String 水题
B. Equidistant String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
随机推荐
- pytest-fixture之conftest.py
场景: 对于一个py文件中某些用例需要前置条件,某些用例不需要前置条件的情况,使用setup/teardown肯定是不方便的, 这时就需要自定义测试用例的前置条件. 1.fixture优点: 命名不局 ...
- Mybatis核心知识点
一.初识Mybatis框架 mybatis是一个持久层的框架,是apache下的顶级项目. mybatis托管到goolecode下,再后来托管到github下(https://github.com/ ...
- mybatis-plus热部署mapper.xml插件JRebel MybatisPlus extension,报错:java.lang.NullPointerException
事件 mybatis转mybatis-plus,结果原来的Jrebel for intrllij 不能热部署mapper.xml文件,百度得知得添加新的插件 JRebel MybatisPlus ex ...
- Eclipse创建JSP、HTML、CSS文件默认字符集设置成UTF-8
问题:在使用eclipse的时候总是发现新创建的JSP文件.HTML文件等默认总是ISO-8859-1,每次都要修改成自己使用的utf-8的,很是麻烦,因此在网上查看了一下发现是可以修改字符集的默认值 ...
- C++——动态内存分配2-创建对象数组
//创建对象数组 #include<iostream> using namespace std; class Point { public: Point() { ...
- 执行python程序的方式
1.交互器 程序不能永久保存 主要用于简单的语法测试相关 2.文件执行
- 一起学Vue之列表渲染
在Vue开发中,列表数据绑定非常简单易用,本文主要通过一些简单的小例子,讲述v-for的使用方法,仅供学习分享使用,如有不足之处,还请指正. 用 v-for 把一个数组对应为一组元素 我们可以用 v- ...
- 【Unity|C#】基础篇(6)——const、readonly、static readonly
[学习资料] <C#图解教程>(第6章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu.c ...
- Tp5整理
一.命名规则 目录级和文件命名 目录采用小写字母+下划线命名: 类文件名采用驼峰法命名(比如:ArticleDetail.php),其它文件与目录命名规则同: 类名与类文件名须保持一致,采用驼峰法: ...
- AM335X的应用程序自启动流程以及U盘更新应用程序记录
在AM335X的SD卡更新系统学习记录中最后更新完系统后,以后运行应用程序都会从EMMC中取出Linux系统运行.接着介绍Linux系统是怎么自己启动我们编写的应用程序的. 1.在AM335X的SD卡 ...