Problem 905
Minimum Number of K Consecutive Bit Flips
Hard · 30 pts · Array, Sliding Window, Queue, Bit Manipulation
Problem
A K-bit flip chooses a block of exactly K consecutive elements of a binary array and flips every bit in it (0 becomes 1 and 1 becomes 0). Find the minimum number of flips that turns every element into 1, or print -1 if it is impossible. Going from left to right, the leftmost 0 can only be fixed by a flip starting there, so the greedy choice is forced; track the active flips with a queue so each position knows its current parity in O(1).
Input
The first line contains N and K. The second line contains the N bits.
Output
Print the minimum number of flips, or -1.
Constraints
1 <= K <= N <= 100000; every value is 0 or 1
Example input
3 1 0 1 0
Example output
2
Explanation
For 0 1 0 with K = 1, flip index 0 and index 2 separately, so the answer is 2. For 1 1 0 with K = 2 it is impossible, because the last 0 cannot be flipped alone.