A12263 | Heaters
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_i \le 1$ ) — the Vova's house description.
输出格式
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
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted