CF1066B.Heaters
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
沃瓦的家是一个由 n 个元素组成的数组(没错,这是第一道题,我想,有人住在数组里)。数组的某些位置上装有暖气片。若第 i 个位置上装有暖气片,则数组的第 i 个元素为 1;否则为 0。
每个暖气片都有一个参数值 r(所有暖气片的 r 值相同)。该值表示:位于位置 pos 的暖气片可以加热区间 [pos−r+1,pos+r−1] 内的所有元素。
沃瓦喜欢在家中边散步边思考,他非常讨厌家里有寒冷的位置。因此,沃瓦希望打开其中一部分暖气片,使得家中的每个位置至少被一个暖气片加热。
沃瓦的目标是加热整栋房子(即加热数组的所有元素)。例如,若 n=6,r=2,且暖气片位于位置 2 和 5,那么沃瓦只需打开家中全部暖气片,即可实现全屋加热(此时前 3 个元素由第一个暖气片加热,后 3 个元素由第二个暖气片加热)。
初始状态下,所有暖气片均处于关闭状态。
但另一方面,沃瓦又不愿为电费支付过多开销。因此,他希望以最少的开启数量,使得家中的每个位置至少被一个暖气片加热。
你的任务是求出所需开启的暖气片的最小数量;若无法加热整栋房子,则输出不可能。
输入格式
The first line of the input contains two integers n and r (1≤n,r≤1000) — the number of elements in the array and the value of heaters.
The second line contains n integers a1,a2,…,an (0≤ai≤1) — the Vova's house description.
输入的第一行包含两个整数 n 和 r(1≤n,r≤1000)——分别表示数组的元素个数和加热器的覆盖半径。
第二行包含 n 个整数 a1,a2,…,an(0≤ai≤1)——描述 Vova 的房屋布局。
输出格式
Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
输出一个整数——即加热整个房屋所需的最少加热器数量;如果无法实现,则输出 −1。
输入输出样例
输入#1
6 2 0 1 1 0 0 1
输出#1
3
输入#2
5 3 1 0 0 0 1
输出#2
2
输入#3
5 10 0 0 0 0 0
输出#3
-1
输入#4
10 3 0 0 1 1 0 1 0 0 0 1
输出#4
3
说明/提示
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.
在第一个例子中,位于位置 2 的加热器覆盖区间 [1;3],位于位置 3 的加热器覆盖区间 [2,4],位于位置 6 的加热器覆盖区间 [5;6],因此答案为 3。
在第二个例子中,位于位置 1 的加热器覆盖区间 [1;3],位于位置 5 的加热器覆盖区间 [3;5],因此答案为 2。
在第三个例子中,没有加热器,因此答案为 −1。
在第四个例子中,位于位置 3 的加热器覆盖区间 [1;5],位于位置 6 的加热器覆盖区间 [4;8],位于位置 10 的加热器覆盖区间 [8;10],因此答案为 3。
输入解题思路,AI测评打分。不知道怎么写?