#C8255. Minimum Operations to Avoid Consecutive Empty Flower Beds
Minimum Operations to Avoid Consecutive Empty Flower Beds
Minimum Operations to Avoid Consecutive Empty Flower Beds
You are given an integer \(n\) representing the number of flower beds and an array of \(n\) integers that indicate whether a flower is present (1) or absent (0) in a bed. Your task is to determine the minimum number of operations required to ensure that no two adjacent flower beds are both empty.
In one operation, you can choose a contiguous segment of empty flower beds starting from the first occurrence of two consecutive zeros and flip the state of each bed in that segment (i.e., change 0 to 1, or 1 to 0). The operation continues on the segment until a non-empty (1) flower bed is encountered. If it is impossible to achieve a configuration where no two consecutive flower beds are empty, output \(-1\).
If the arrangement already meets the condition (i.e., there are no adjacent zeros), then no operation is needed and the answer is 0.
inputFormat
The first line contains an integer \(n\) representing the number of flower beds.
The second line contains \(n\) space-separated integers, each being either 0 or 1, which represent the state of the flower beds.
outputFormat
Output a single integer: the minimum number of operations required to ensure that no two consecutive flower beds are empty. If it is impossible to achieve, output \(-1\).
## sample5
1 0 0 1 0
1