You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1').c++
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.ide
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.spa
You have to answer q independent test cases.code
The first line of the input contains one integer q (1≤q≤104) — the number of test cases.orm
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.ci
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).input
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.string
input
3
8 5
11011010
7 9
1111100
7 11
1111100
output
01011110
0101111
0011111it
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(); }