Binary String
Contest: Educational Codeforces Round 128 (Rated for Div. 2)
Difficulty rating: 1600
Tags: binary search, greedy, strings, two pointers

You are given a string $$$s$$$ consisting of characters 0 and/or 1.

You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:

- the number of characters 0 left in the string;
- the number of characters 1 removed from the string.

What is the minimum cost of removal you can achieve?

Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

Each test case consists of one line containing the string $$$s$$$ ($$$1 \le |s| \le 2 \cdot 10^5$$$), consisting of characters 0 and/or 1.

The total length of strings $$$s$$$ in all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, print one integer — the minimum cost of removal you can achieve.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
Consider the test cases of the example:

1. in the first test case, it's possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is $$$1$$$;
2. in the second test case, it's possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is $$$3$$$;
3. in the third test case, it's optimal to remove four characters from the beginning;
4. in the fourth test case, it's optimal to remove the whole string;
5. in the fifth test case, it's optimal to leave the string as it is.

Example input:
5
101110110
1001001001001
0000111111
00000
1111

Example output:
1
3
0
0
0

Editorial:
There are many different approaches to this problem: dynamic programming, binary search, greedy, two pointers, anything you want. The model solution uses an approach based on binary search, so I'll describe it.
First of all, why does binary search work? Let's say that the number of 1's is $$$c$$$. If the cost of deletion is $$$k$$$, then we have deleted at most $$$k$$$ characters 1, and have left at most $$$k$$$ characters 0. Let's increase the number of characters we delete from the prefix of the string until the number of deleted 1's becomes $$$k+1$$$: if $$$c \ge k+1$$$, it's always possible. So, if we consider the segment of values $$$[0, c]$$$, the fact that we can get cost $$$k$$$ implies that we can get cost $$$k+1$$$, so we can use binary search on segment $$$[0, c]$$$ to find the minimum achievable cost.
Now, how to check if we can obtain the cost of deletion equal to $$$k$$$? One possible way to do this is to form an array $$$pos$$$, where $$$pos_i$$$ is the position of the $$$i$$$-th character 1 in the string, and find the minimum value of $$$pos_{i+c-k-1} - pos_i$$$ in this array: the string that should remain has to contain at least $$$c-k$$$ characters 1, and the minimum value of $$$pos_{i+c-k-1} - pos_i$$$ is the minimum possible length of such string. Then we can find the number of 0's in this string and check if it is greater than $$$k$$$ or not.