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 n 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 k 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 i and i+1 arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1≤q≤104) — the number of test cases.
The first line of the test case contains two integers n and k (1≤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 n characters '0' and '1'.
It is guaranteed that the sum of n over all test cases does not exceed 106 (∑n≤106).
Output
For each test case, print the answer on it: the lexicographically minimum possible string of length n you can obtain from the given one if you can perform no more than k moves.
Example
input
3
8 5
11011010
7 9
1111100
7 11
1111100
output
01011110
0101111
0011111
Note
In the first example, you can change the string as follows: 110–––11010→10–––111010→011110–––10→01110–––110→0110–––1110→01011110.
In the third example, there are enough operations to make the string sorted.
题意
现在有t组数据,每组数据给你n和k;表示二进制的长度和操作次数。
每次操作你可以选择一个数和他相邻的位置进行交换,在不超过k次的操作次数情况下,使得这个字符串变得最小。
题解
贪心,每次操作最小的数,使得他尽可能的放在前面;由于里面只有0和1,其实就是操作0,把0尽可能的往前面放。
代码
#include<bits/stdc++.h>
using namespace std;
int n;
long long k;
string s;
vector<int>p;
void solve(){
scanf("%d%lld",&n,&k);
cin>>s;
p.clear();
for(int i=0;i<s.size();i++){
if(s[i]=='0'){
p.push_back(i);
}
}
int la=-1;
for(int i=0;i<p.size();i++){
// cout<<"before "<<p[i]<<" "<<k<<endl;
if(k>p[i]-la-1){
int cost=p[i]-la-1;
k-=cost;
p[i]=la+1;
la=p[i];
}else{
p[i]-=k;
break;
}
//cout<<"aft "<<p[i]<<" "<<k<<endl;
}
vector<int>o(s.size(),1);
for(int i=0;i<p.size();i++){
o[p[i]]=0;
}
for(int i=0;i<o.size();i++){
cout<<o[i];
}
cout<<endl;
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
}
Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心的更多相关文章
- 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'). ...
- Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)
题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...
- Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心
B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...
- 贪心 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 #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
随机推荐
- Vim 命令常用功能详解
Vim编辑器 文本编辑器 , 字处理器ASCIIvi:Visual Interface vim :VI iMproved 全屏编辑器,模式化编辑器vim 模式:编辑模式(命令模式)输入模式末行模式 模 ...
- [Go]TCP服务中读写进行协程分离
读写两部分进行一下分离,中间通过chan进行传递数据 ,这样可以方便的在write中进行一些业务处理 single/snet/tcpconn.go package snet import ( &quo ...
- sqlldr bat遇到的问题
在编写sqlldr相关的bat脚本时,遇到执行bat后一直循环执行的问题,网上也有遇到相同问题的朋友: 链接:https://zhidao.baidu.com/question/17039912443 ...
- python之os模块(os.path)
我们在做自动化测试的时候,可能会遇到一些需要处理文件一些需求,那么我们可以通过直接写文件的目录进行操作,当然作为一名自动化测试工程师,怎么可能用这种方法?python中自带的有OS,我们可以通过os模 ...
- 用iText5-1-生成PDF
参考代码和图片出处 https://howtodoinjava.com/library/read-generate-pdf-java-itext/ pom引入jar包 <dependencies ...
- 08. Go 语言包(package)
Go 语言包(package) Go 语言的源码复用建立在包(package)基础之上.Go 语言的入口 main() 函数所在的包(package)叫 main,main 包想要引用别的代码,必须同 ...
- [IDA] 自动下载符号
当现实无法自动下载符号时,看下面交互窗口,提示安装 VC++ 2008. 安装成功之后就会自动下载符号.
- 立 Flag
行动目标 立Flag时间 计划开始时间 开始时间 行动寄语 通关目标 打卡1 打卡2 打卡3 打卡4 打卡5 打卡6 C# 7.0 核心技术指南 2019-11-1 1号 1号 打好C#基础 看完.实 ...
- Web前端基础(14):jQuery基础(一)
1. jQuery概述 1.1 为什么要使用jQuery 在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题. ...
- 彻底理解volatile,领悟其中奥妙
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...