Heaters
Contest: Codeforces Round 515 (Div. 3)
Difficulty rating: 1500
Tags: greedy, two pointers

Vova's house is an array consisting of $$$n$$$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $$$i$$$-th element of the array is $$$1$$$ if there is a heater in the position $$$i$$$, otherwise the $$$i$$$-th element of the array is $$$0$$$.

Each heater has a value $$$r$$$ ($$$r$$$ is the same for all heaters). This value means that the heater at the position $$$pos$$$ can warm up all the elements in range $$$[pos - r + 1; pos + r - 1]$$$.

Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.

Vova's target is to warm up the whole house (all the elements of the array), i.e. if $$$n = 6$$$, $$$r = 2$$$ and heaters are at positions $$$2$$$ and $$$5$$$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $$$3$$$ elements will be warmed up by the first heater and the last $$$3$$$ elements will be warmed up by the second heater).

Initially, all the heaters are off.

But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.

Your task is to find this number of heaters or say that it is impossible to warm up the whole house.

Input format:
The first line of the input contains two integers $$$n$$$ and $$$r$$$ ($$$1 \le n, r \le 1000$$$) — the number of elements in the array and the value of heaters.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 1$$$) — the Vova's house description.

Output format:
Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example the heater at the position $$$2$$$ warms up elements $$$[1; 3]$$$, the heater at the position $$$3$$$ warms up elements $$$[2, 4]$$$ and the heater at the position $$$6$$$ warms up elements $$$[5; 6]$$$ so the answer is $$$3$$$.

In the second example the heater at the position $$$1$$$ warms up elements $$$[1; 3]$$$ and the heater at the position $$$5$$$ warms up elements $$$[3; 5]$$$ so the answer is $$$2$$$.

In the third example there are no heaters so the answer is -1.

In the fourth example the heater at the position $$$3$$$ warms up elements $$$[1; 5]$$$, the heater at the position $$$6$$$ warms up elements $$$[4; 8]$$$ and the heater at the position $$$10$$$ warms up elements $$$[8; 10]$$$ so the answer is $$$3$$$.

Example input:
6 2
0 1 1 0 0 1

Example output:
3

Example input:
5 3
1 0 0 0 1

Example output:
2

Editorial:
Let's solve this problem greedily. Let $$$last$$$ be the last position from the left covered by at least one heater. Initially, $$$last$$$ equals -1. While $$$last < n - 1$$$, lets repeat the following process: firstly, we have to find the rightmost heater in range $$$(max(-1, last - r + 1); last + r]$$$. It can be done in time $$$O(n)$$$ because of given constrains or in $$$O(1)$$$ using precalculated prefix values for each $$$i$$$ in range $$$[0; n - 1]$$$. If there is no such heater then the answer is -1, otherwise let's set $$$last := pos + r - 1$$$, increase the answer by $$$1$$$ and repeat the process if $$$last < n - 1$$$.
There is another one solution to this problem. Assume that the initial answer equals to the total number of heaters. Let's calculate an array $$$cnt$$$ of length $$$n$$$, where $$$cnt_i$$$ means the number of heaters covering the $$$i$$$-th element. It can be done in $$$O(n^2)$$$. This array will mean that we are switch all the heaters on and we know for each element the number of heaters covers this element. Now if for at least $$$i \in [0, n - 1]$$$ holds $$$cnt_i = 0$$$ then the answer is -1. Otherwise let's switch useless heaters off. Let's iterate over all heaters from left to right. Let the current heater have position $$$i$$$. We need to check if it is useless or not. Let's iterate in range $$$[max(0, i - r + 1), min(n - 1, i + r - 1)]$$$ and check if there is at least one element $$$j$$$ in this segment such that $$$cnt_j = 1$$$. If there is then the current heater is not useless and we cannot switch it off. Otherwise we can decrease the answer by $$$1$$$, switch this heater off (decrease $$$cnt_j$$$ for all $$$j$$$ in range $$$[max(0, i - r + 1), min(n - 1, i + r - 1)]$$$) and continue the process.