Dr. Anne Anderson Coding Contest 1 P4 - Lighting

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types

Alex's social studies class just finished watching a video, so the lights in the classroom are dimmed. As he sits next to the light switches, his teacher asks him to turn them on.

His classroom is lit by one column of N lights, spaced one metre apart. The lights can be represented by a binary string S, where at each index 1 represents an on light and 0 represents an off light. Each student sits directly below a light, and for each student to have enough light to work, there must be at least one light on within K metres from the student in either direction.

Alex needs to turn lights on to give all students enough light, but his fingers are sore from too much programming, so he doesn't want to flip more switches than he needs to. Given the current configuration of light switches, can you determine the minimum number of lights he has to turn on?

Constraints

For all subtasks:

N = |S|

1 \leq K \leq N \leq 10^6

Subtask 1 [20%]

No lights will be turned on initially.

Subtask 2 [30%]

The row will start and end with an on light.

Subtask 3 [20%]

K = 1

Subtask 4 [30%]

No additional constraints.

Input Specification

The first line will contain the integer N.

The second line contains the integer K.

The third line contains the string S.

Output Specification

The output will contain one integer, the minimum number of lights Alex has to turn on.

Sample Input

16
2
0001100000000001

Sample Output

3

Explanation for Sample

The unmodified configuration results in some students not having enough light. The distance of each student from the nearest on light is shown below:

Light Configuration:  0001100000000001
Distance from Light:  3210012345543210
Enough Light:         NYYYYYYNNNNNNYYY

It can be proven that at least three lights must be turned on to give all students enough light. The following is an example of a correct configuration using three flips:

Light Configuration:  1001100001001001
Distance from Light:  0110012210110110
Enough Light:         YYYYYYYYYYYYYYYY

Comments

There are no comments at the moment.