#K39782. Minimum Toggle Operations
Minimum Toggle Operations
Minimum Toggle Operations
You are given a row of n lights, each either on (1) or off (0). Your task is to determine the minimum number of toggle operations needed to turn off all the lights.
A toggle operation works as follows: starting with an initial expected state of off (0), iterate through the lights. Whenever you encounter a light whose current state does not match the expected state, perform a toggle operation and flip the expected state. This process can be thought of as correcting the discrepancy. The operation count is the number of toggles performed.
Mathematically, let \(L = [l_1, l_2, \dots, l_n]\) be the list of lights and let the initial expected state be \(s_0 = 0\). For each light \(l_i\), if \(l_i \neq s_{i-1}\), then increase the toggle count by 1 and set \(s_i = 1 - s_{i-1}\); otherwise, set \(s_i = s_{i-1}\). The answer is the total toggle count.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains an integer \(n\) \((1 \leq n \leq 10^5)\), representing the number of lights.
- The second line contains \(n\) space-separated integers, each either 0 or 1, representing the state of each light.
outputFormat
Output a single integer to stdout which is the minimum number of toggle operations required to turn off all the lights.
## sample4
0 0 0 0
0