A. Shift Sort
Problem Statement
You are given a binary string of length . You may perform the following operation any number of times (including zero):
- Choose three indices and cyclically shift the values at , , either to the right or left.
For example, for the string :
- If you choose , , and perform a right shift, the string becomes .
- If you choose , , and perform a left shift, the string becomes .
Your task is to determine the minimum number of operations required to sort the binary string in non-decreasing order (all s before all s).
Solution
Key Observations
- Operation Effect: Each operation allows you to cyclically shift three elements, which can be used to move a ‘0’ past two ‘1’s or vice versa.
- Sorting Goal: The goal is to move all ‘0’s to the front and all ‘1’s to the back.
- Optimal Strategy: The minimum number of ‘0’ not in the correct position. Each operation can swap a pair of ‘0’ and ‘1’.