#C3344. Minimum Operations to Transform Binary String into All Ones
Minimum Operations to Transform Binary String into All Ones
Minimum Operations to Transform Binary String into All Ones
You are given a binary string s
of length n
. Your task is to determine the minimum number of operations required to convert the string into one where every character is '1'
.
An operation is defined as follows:
- Traverse the string and count the number of positions
i
(with1 ≤ i < n
) where the character at positioni
differs from the character at positioni-1
. - If there is at least one
'0'
in the string, add an additional operation.
In other words, if you count the number of transitions between consecutive characters and then, if the string is not already all '1'
s, add one more operation, you will have the minimum number of operations needed.
The answer should be computed based on the provided n and s, and printed as an integer.
Note: All formulas in this problem description use LaTeX syntax. For example, the transition counting can be thought of as:
\( \text{operations} = \sum_{i=1}^{n-1} \mathbf{1}_{\{ s[i] \neq s[i-1] \}} + \mathbf{1}_{\{ \exists i: s[i] = 0 \}} \)
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer
n
(the length of the binary string). - The second line contains a binary string
s
of lengthn
.
outputFormat
The output is a single integer printed to standard output (stdout) representing the minimum number of operations required to transform the string s
into a string of all '1'
s.
5
11001
3