题目B-Competition - 20181017

题目

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.

扫描二维码关注公众号,回复: 3739015 查看本文章

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

6 2
0 1 1 0 0 1

Output

3

Input

5 3
1 0 0 0 1

Output

2

Input

5 10
0 0 0 0 0

Output

-1

Input

10 3
0 0 1 1 0 1 0 0 0 1

Output

3

Note

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

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

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

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

 题意:一个由0,1组成的数组,0为空,1为热源,热源的范围是r,即array[ i - r + 1 ] ~array[ i + r - 1 ]都可以被温暖。热源位置固定,但你可决定其用或者不用,若不可使全部的数组温暖,输出“-1”,若可以使全部的数组温暖,输出最小的打开的热源的数目。

思路:一眼看到觉得是贪心,一想,如果以当前和下一热源为判断依据,最后的那几格怎么判断,最后一个怎么决定去留?然后觉得是一个双重循环,寻找当前热源可辐射的最大位置,确定下一热源,写到最后,又卡住了,原因就是最后几步的特判怎么都写不明白,我以为我换了一种思路,但其实我就是在用贪心,出现了问题。匆匆忙忙打补丁,怎么都过不了,最后删除重写,依然错,,一道水题用了一个多小时,难受,,;

 代码:

#include<bits/stdc++.h>
using namespace std;
int a[1010];
int main()
{
    int n,r,ans = 0,t = 0;
    scanf("%d %d",&n,&r);
    for (int i = 0;i < n;i ++) scanf("%d",&a[i]);
    while (t < n)
    {
        int idx = -1;
        for (int i = 0;i < n;i ++)
            if (a[i])
                if (i - r + 1 <= t && i + r - 1 >= t)
                    idx = i;//能覆盖上一次覆盖位置的最远的热水器的位置
        if (idx == -1)
        {
            puts("-1\n");
            return 0;
        }
        ans ++;
        t = idx + r;//当前的最远覆盖位置
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sodacoco/article/details/83119473
今日推荐