http://codeforces.com/contest/1066/problem/B

Vova's house is an array consisting of nn 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 ii-th element of the array is 11 if there is a heater in the position ii, otherwise the ii-th element of the array is 00.

Each heater has a value rr (rr is the same for all heaters). This value means that the heater at the position pospos can warm up all the elements in range [pos−r+1;pos+r−1][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=6n=6, r=2r=2 and heaters are at positions 22 and 55, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 33 elements will be warmed up by the first heater and the last 33 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

The first line of the input contains two integers nn and rr (1≤n,r≤10001≤n,r≤1000) — the number of elements in the array and the value of heaters.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1) — the Vova's house description.

Output

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

Examples
input

Copy
6 2
0 1 1 0 0 1
output

Copy
3
input

Copy
5 3
1 0 0 0 1
output

Copy
2
input

Copy
5 10
0 0 0 0 0
output

Copy
-1
input

Copy
10 3
0 0 1 1 0 1 0 0 0 1
output

Copy
3

代码:

#include <bits/stdc++.h>
using namespace std; int N, R;
int pos[1010]; int main() {
scanf("%d%d", &N, &R);
for(int i = 0; i < N; i ++)
scanf("%d", &pos[i]); vector<int> T;
int st = -1;
for(int i = N - 1; i >= 0; i --) {
if(pos[i] == 1 && i < R) {
st = i;
break;
}
} if(st == -1) {
printf("-1\n");
return 0;
} if(N - st <= R) {
printf("1\n");
return 0;
} T.push_back(st); while(true) {
// cout << "!!!" << endl;
bool flag = false;
for(int i = N - 1; i >= st + 1; i --) {
if(i - st + 1 <= 2 * R && pos[i]) {
st = i;
flag = true;
break;
}
}
if(!flag) {
break;
} T.push_back(st); if(N - st <= R) {
printf("%d\n", T.size());
return 0;
}
} printf("-1\n"); return 0;
}

  

CodeForces Round #515 Div.3 B. Heaters的更多相关文章

  1. Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】

    任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...

  2. Codeforces Round #515 (Div. 3) B. Heaters (贪心)

    题意:有\(n\)个桩子,\(1\)表示该位置有一个火炉,可以使两边距离为\(r\)的范围照亮,问最少使用多少炉子使得所有范围都被照亮. 题解:贪心,首先我们从\(r\)位置开始向左找,如果找到了就记 ...

  3. Codeforces Round #515 (Div. 3)

    Codeforces Round #515 (Div. 3) #include<bits/stdc++.h> #include<iostream> #include<cs ...

  4. B. Heaters ( Codeforces Round #515 (Div. 3) )

    题解:对于每个点 i 来说,从 j = i + r - 1 开始往前找,如果找到一个 a [ j ] 是 1 ,那么就把它选上,但是我们需要判断交界处,也就是如果前面选的那个可以让这个点变温暖,就不用 ...

  5. Codeforces Round #515 (Div. 3) 解题报告(A~E)

    题目链接:http://codeforces.com/contest/1066 1066 A. Vova and Train 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在 ...

  6. Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum

    E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...

  7. CodeForces Round #515 Div.3 D. Boxes Packing

    http://codeforces.com/contest/1066/problem/D Maksim has nn objects and mm boxes, each box has size e ...

  8. CodeForces Round #515 Div.3 C. Books Queries

    http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...

  9. CodeForces Round #515 Div.3 A. Vova and Train

    http://codeforces.com/contest/1066/problem/A Vova plans to go to the conference by train. Initially, ...

随机推荐

  1. [VC]VC实现开机自动运行程序

    有时候,我们需要在计算机启动的时候就启动某些程序,不要人干预.这里,提供一种让程序开机自动运行的方法.见下面代码: BOOL CXXX::SetAutoRun(CString strPath) { C ...

  2. Http协议--请求报文和响应报文

           http协议是位于应用层的协议,我们在日常浏览网页比如在导航网站请求百度首页的时候,会先通过http协议把请求做一个类似于编码的工作,发送给百度的服务器,然后在百度服务器响应请求时把相应 ...

  3. 【BZOJ4810】[YNOI2017] 由乃的玉米田(莫队+bitset)

    点此看题面 大致题意: 给你一段序列,每次询问一段区间内是否存在两个数的差或和或积为\(x\). 莫队算法 看到区间询问+可以离线,首先想到了莫队啊. 但是,在较短的时间内更新信息依然比较难以实现. ...

  4. IIS6.0开启gzip压缩

    双击IIS服务器,右键点击网站,点击属性,然后点击服务,我们看到HTTP压缩,然后在压缩应用程序文件,压缩静态文件中打钩,然后点击确定,第一步就完成了   然后我们右键点击web服务扩展,点击添加一个 ...

  5. convert命令

    可以修改图片的分辨率 convert -resize 600×600 src.jpg dst.jpg src.jpg是你要修改的图片的名字 dst.jpg是新生成的图片名字

  6. sass安装更新及卸载方法

    在 Windows 平台下安装 Ruby 需要先有 Ruby 安装包,大家可以到 Ruby 的官网(http://rubyinstaller.org/downloads)下载对应需要的 Ruby 版本 ...

  7. Python基础2-Python中文乱码(转)

    转自:https://blog.csdn.net/apache0554/article/details/53889253 前言:中文编码问题一直是程序员头疼的问题,而Python2中的字符编码足矣令新 ...

  8. struts2、hibernate的知识点

    以下内容是我在复习struts2.hibernate和spring的时候记下得到,部分书上找不到的内容来自网络 以下是网络部分的原文网站: http://blog.csdn.net/frankaqi/ ...

  9. 数据存储之json文件处理和csv文件处理

    什么是json: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (w3c制定的js规范)的一个子集,采用 ...

  10. 数据分析处理库Pandas——索引

    显示DataFrame结构中的指定列 使用iloc索引 指定一行的信息 指定多行信息 备注:第[1,5)行信息. 指定行和列 备注:第[0,5)行中第[1,3)列信息. 使用loc索引 指定行信息 备 ...