#C10568. Minimum Flips to Open Boxes
Minimum Flips to Open Boxes
Minimum Flips to Open Boxes
You are given n boxes arranged in a row. Each box is represented by a binary digit: '1' indicates that the box is open and '0' indicates that it is closed.
In one operation, you are allowed to flip the state of a contiguous segment of boxes. Due to the peculiar mechanism of these boxes, the minimum number of flip operations required to make all boxes open is equal to the number of contiguous segments in the configuration, provided that at least one box is initially closed.
Formally, let the binary string be \( S = S_1S_2\ldots S_n \). If \( S_i = 1 \) for all \( 1 \le i \le n \), then the answer is 0. Otherwise, if there is at least one '0', the answer is computed as follows:
\[ \text{result} = \#\{i : 2 \le i \le n \;\text{and}\; S_i \neq S_{i-1}\} + 1 \]Determine the minimum number of flip operations required to open all boxes.
inputFormat
The input consists of two lines:
- The first line contains an integer
n
— the number of boxes. - The second line contains a binary string of length
n
composed of characters '0' and '1' representing the state of each box.
outputFormat
Output a single integer — the minimum number of flip operations required to open all the boxes.
## sample5
11111
0
</p>