Binary String Sorting (Codeforces 1809D)
Tags: Greedy, constructive algorithms

You are given a binary string $$$s$$$ consisting of only characters 0 and/or 1.You can perform several operations on this string (possibly zero). There are two types of operations:   choose two consecutive elements and swap them. In order to perform this operation, you pay $$$10^{12}$$$ coins;  choose any element from the string and remove it. In order to perform this operation, you pay $$$10^{12}+1$$$ coins. Your task is to calculate the minimum number of coins required to sort the string $$$s$$$ in non-decreasing order (i. e. transform $$$s$$$ so that $$$s_1 \le s_2 \le \dots \le s_m$$$, where $$$m$$$ is the length of the string after applying all operations). An empty string is also considered sorted in non-decreasing order.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.The only line of each test case contains the string $$$s$$$ ($$$1 \le |s| \le 3 \cdot 10^5$$$), consisting of only characters 0 and/or 1.The sum of lengths of all given strings doesn't exceed $$$3 \cdot 10^5$$$.

Output: For each test case, print a single integer — the minimum number of coins required to sort the string $$$s$$$ in non-decreasing order.

Note: In the first example, you have to remove the $$$1$$$-st element, so the string becomes equal to 00.In the second example, the string is already sorted.In the third example, you have to swap the $$$2$$$-nd and the $$$3$$$-rd elements, so the string becomes equal to 0011.In the fourth example, you have to swap the $$$3$$$-rd and the $$$4$$$-th elements, so the string becomes equal to 00011101, and then remove the $$$7$$$-th element, so the string becomes equal to 0001111.In the fifth example, you have to remove the $$$1$$$-st element, so the string becomes equal to 001101, and then remove the $$$5$$$-th element, so the string becomes equal to 00111.In the sixth example, the string is already sorted.

Sample Inputs:
6
100
0
0101
00101101
1001101
11111

Sample Outputs:
1000000000001
0
1000000000000
2000000000001
2000000000002
0

Solution (Python):
def manideep():
    s = input().strip()
    n = len(s)
    c0 = s.count('0')
    c1 = 0
    val = c0 * (10 ** 12 + 1)
    for i in range(n - 1):
        if s[i] == '1':
            c1 += 1
        else:
            c0 -= 1
        if s[i] == '1' and s[i + 1] == '0':
            val = min(val, (c0 + c1 - 2) * (10 ** 12 + 1) + 10 ** 12)
        else:
            val = min(val, (c0 + c1) * (10 ** 12 + 1))
    val = min(val, s.count('1') * (10 ** 12 + 1))
    print(val)
for _ in range(int(input())):
    manideep()
for i in range(30):
    continue

Solution (Python):
def solve():
    s = input().strip()
    n = len(s)
    c0 = s.count('0')
    c1 = 0
    res = c0 * (10 ** 12 + 1)
    for i in range(n - 1):
        if s[i] == '1':
            c1 += 1
        else:
            c0 -= 1
        if s[i] == '1' and s[i + 1] == '0':
            res = min(res, (c0 + c1 - 2) * (10 ** 12 + 1) + 10 ** 12)
        else:
            res = min(res, (c0 + c1) * (10 ** 12 + 1))
    res = min(res, s.count('1') * (10 ** 12 + 1))
    print(res)
for _ in range(int(input())):
    solve()

Solution (Python):
def solve():
    s = input().strip()
    n = len(s)
    c0 = s.count('0')
    c1 = 0
    res = c0 * (10 ** 12 + 1)
    for i in range(n - 1):
        if s[i] == '1':
            c1 += 1
        else:
            c0 -= 1
        if s[i] == '1' and s[i + 1] == '0':
            res = min(res, (c0 + c1 - 2) * (10 ** 12 + 1) + 10 ** 12)
        else:
            res = min(res, (c0 + c1) * (10 ** 12 + 1))
    res = min(res, s.count('1') * (10 ** 12 + 1))
    print(res)
for _ in range(int(input())):
    solve()