#K93082. Minimizing Zeros After One Substring Flip
Minimizing Zeros After One Substring Flip
Minimizing Zeros After One Substring Flip
You are given a binary string s
of length n consisting solely of characters '0' and '1'. In one operation, you must choose a contiguous substring of s
and flip all its bits (i.e. change '0' to '1' and '1' to '0'). Your task is to determine the minimum number of '0's that can be obtained after performing exactly one such flip operation.
The flipping operation on a substring s[l..r]
transforms it such that:
[ \text{new zeros in } s[l..r] = \text{(number of ones in } s[l..r] ) ]
Thus, the overall number of zeros in the entire string becomes:
[ \text{zeros}{\text{final}} = \text{zeros}{\text{initial}} - (\text{number of zeros in } s[l..r]) + (\text{number of ones in } s[l..r]) ]
Your goal is to choose a substring to flip that minimizes \(\text{zeros}_{\text{final}}\).
Note: It is mandatory to perform exactly one flip operation even if it does not improve the situation.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains an integer n — the length of the binary string.
- The second line contains the binary string
s
of length n, consisting only of '0's and '1's.
outputFormat
Output to stdout a single integer representing the minimum number of '0's that can be achieved after exactly one flip operation.
## sample5
11001
0
</p>