CodeForces--D:Binary String Minimizing

D. Binary String Minimizing

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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

inputCopy

3
8 5
11011010
7 9
1111100
7 11
1111100

outputCopy

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.

翻译

给你一个二进制的数,让其数最大。有k次可以让相邻两位数字交换的机会,不限位置。

#include<stdio.h>
#define max(a,b) a>b?a:b
int main()
{
	int q;
	scanf("%d",&q);
	while(q--){
		long long n,k;
		char num[1000000+20];
		scanf("%lld%lld%s",&n,&k,num);
		int last=0;
		for(int i=0;i<n;i++){
			int index=max(last,i-k);
			if(k==0) break;
			if(num[i]=='0'){
				if(num[index]=='1'){
					k=k+index-i;
					num[index]='0';
					num[i]='1';	
				}
				last=index+1;
			}
		}
		puts(num);
	}
	return 0;
}

其实就是简单的贪心,能往前提就往前提,考虑到是对每一位都这样做,所以已经扫过的数字有两种情况,第一种,k不够大,这时因为k只会不断减小,所以前面i-k的位置一定有一个可以往前提的位置。第二种,k足够大,这时前面一定是1堆积在前面的情况,例如1111000000。这时我们选取上一次操作过的下一位也即last即可。考虑到last的优先级应该大于i-k(已经操作过的为什么还要去动),所以每次选的index应该为max(i-k,last)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值