Binary String Minimizing (Codeforces 1256D)
Tags: Greedy, greedy

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 \le q \le 10^4$$$) — the number of test cases.The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^6, 1 \le k \le n^2$$$) — 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 $$$10^6$$$ ($$$\sum n \le 10^6$$$).

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.

Note: In the first example, you can change the string as follows: $$$1\underline{10}11010 \rightarrow \underline{10}111010 \rightarrow 0111\underline{10}10 \rightarrow 011\underline{10}110 \rightarrow 01\underline{10}1110 \rightarrow 01011110$$$. In the third example, there are enough operations to make the string sorted.

Sample Inputs:
3
8 5
11011010
7 9
1111100
7 11
1111100

Sample Outputs:
01011110
0101111
0011111

Solution (Python):
def solve(n, k, s):
    o = z = 0
    for (i, c) in enumerate(s):
        if not k:
            return '0' * z + '1' * o + s[i:]
        if c == '1':
            o += 1
        elif k >= o:
            k -= o
            z += 1
        else:
            d = o - k
            return '0' * z + '1' * d + '0' + '1' * k + s[i + 1:]
    return '0' * z + '1' * o
for _ in range(int(input())):
    (n, k) = map(int, input().split())
    s = input()
    print(solve(n, k, s))

Solution (Python):
for _ in range(int(input())):
    (n, k) = map(int, input().split())
    s = input()
    (item, res, case) = (0, '', 0)
    for (i, x) in enumerate(s):
        if x == '1':
            if case == 0:
                ind = i
            item += 1
        elif k >= item:
            k -= item
        else:
            if k == 0:
                elem = 1
            else:
                elem = item - k
            res += (i - item) * '0' + (item - k) * '1' + '0' + k * '1' + s[i + 1:]
            print(res)
            break
    if res == '':
        print('0' * (n - item) + '1' * item)

Solution (Python):
import math
import sys
q = int(input())
for _ in range(q):
    (n, k) = map(int, input().split())
    s = str(input())
    counter = 0
    moves_remain = k
    zeros = 0
    r = ''
    midlzero = 0
    left = 0
    right = 0
    for i in range(len(s)):
        if s[i] == '1':
            counter += 1
        elif counter <= moves_remain:
            zeros += 1
            moves_remain -= counter
        else:
            r = s[i + 1:len(s)]
            right = moves_remain
            midlzero = 1
            counter -= right
            break
    print('0' * zeros + '1' * counter + '0' * midlzero + '1' * right + r)