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 贪心的更多相关文章

  1. 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'). ...

  2. Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)

    题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...

  3. 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 ...

  4. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  5. Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划

    Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...

  6. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  7. Codeforces Round #598 (Div. 3)

    传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...

  8. 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 ...

  9. 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  ...

随机推荐

  1. HTML入门(列表、表单、常用表单控件、浮动框架、iframe、 摘要与细节、度量标签)

    一.列表 1.作用:默认显示方式为从上到下的显示数据 2.列表的组成 列表类型和列表项 3.列表的分类:有序列表   无序列表   自定义列表 无序列表语法为ul>li, 语法:ul代表列表,l ...

  2. 2019 DevOps 技术指南

    原文链接:https://hackernoon.com/the-2018-devops-roadmap-31588d8670cb 原文作者:javinpaul 翻译君:CODING 戴维奥普斯 写在前 ...

  3. Hive 时间函数总结【转】

    1.日期函数UNIX时间戳转日期函数: from_unixtime语法:from_unixtime(bigint unixtime[, stringformat]) 返回值: string说明: 转化 ...

  4. [洛谷P1169][题解][ZJOI2007]棋盘制作

    我不是题目的说 这道题运用了一种很巧妙的DP方式:悬线法 如图,蓝色为悬线,黄色为向两边延伸的长度 那么显然,最大子矩形的宽一定是这些黄线中最小的(证明从略) 所以我们可以维护三个数组: Up[i][ ...

  5. Httpclient4.5.*HttpClient请求,对于新建httpclient实例时保持会话

    package net.bill99.httpconsel; import java.io.IOException; import java.util.*; import java.util.Map. ...

  6. md2all 简单实用

    . 简单使用 md2all 使用地址:http://md.aclickall.com/ . 常用操作 #代表标题等级 英文下```java(语言)``` 代表代码块

  7. 解决python安装第三方库超时问题

    这里说明一下,配置文件中的url还可以换成下面的URL 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.u ...

  8. ETCD:gRPC命名与发现

    原文地址:gRPC naming and discovery etcd提供一个gRPC解析器支持备用的命名系统,该命名系统从etcd获取主机以发现gRPC服务.以下机制基于监视对以服务名称为前缀的Ke ...

  9. .net Core数据的幕等性

    一.背景 代码实例:https://gitee.com/D_C_L/CurtainEtcAOP.git我们实际系统中有很多操作,是不管做多少次,都应该产生一样的效果或返回一样的结果. 例如: 1. 前 ...

  10. tomcat8 到idea控制台和servlet乱码问题

    作者:晨钟暮鼓c个人微信公众号:程序猿的月光宝盒 1.问题重现 ​ Tomcat8 部署到idea上时候,控制台出现的乱码 如图,本来框出来的是乱码 其中,"测试"这个是在serv ...