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. js-03-if条件判断

    一.swich case判断语句eg <script> swich( exp){ case 值1: 执行的代码段: break; case 值2: 执行的代码段: break; ..... ...

  2. Oracle number类型前端界面和数据库查询不一致 number精度问题

    [发现问题] [问题分析] Ⅰ.在前端界面查询,发现了库存中存在这样的数量值.但是在数据库中查询时显示正常.即6.999999999999997 为 7. Ⅱ.至于这种小数产生,我以为是oracle存 ...

  3. 2019 DevOps 技术指南

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

  4. Shell—脚本编程进阶

    shell脚本进阶之条件语句 条件选择if语句 https://www.runoob.com/?s=shell&page=1 https://www.cnblogs.com/flylinux/ ...

  5. vue介绍以及相关概念理解大全

    1.什么是vue 以官网的解释来说,vue是渐进式javascript框架.Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计 ...

  6. Github新项目Dress(好耶是女装)

    面向可爱的蓝孩子 (/ω\) 的 git 学习实践项目,包含了600多位蓝孩子的女装照,访问前做好心理准备. 每个人都可以成为这个项目的参与者 访问地址:https://github.com/kome ...

  7. [C++]Game模板-正面视角

    前言 本来是想打一个小游戏的-- 可是打到一半思路断了-- 只打出了模板--先把模板拿出来放着 Code //head #include <iostream> #include <c ...

  8. npm与cnpm的区别

    NPM(Node Package Manager,节点包管理器)是NodeJS的包管理器,用于节点插件的管理(包括安装,卸载和管理依赖等).NPM是随同新版的NodeJS一起安装的包管理工具,所以我们 ...

  9. 连接SpringBootAdmin 异常 Name or service not known

    环境: 服务器:Linux 客户端:Windows 当客户端连接到SpringBootAdmin的时候,报了如下异常 明明显示可以连接了,但是就是报了客户端INSTANCE DOWN   (down机 ...

  10. 一起学Android之Animation

    本文以一个简单的小例子,简述在Android开发中,动画的简单应用,仅供学习分享使用. 概述 android提供了各种强大的apis,用于将动画应用到ui元素中,来丰富应用程序的功能和应用. 动画分类 ...