I - Heaters CodeForces - 1066B

B. Heaters
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Input
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.

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
inputCopy
6 2
0 1 1 0 0 1
outputCopy
3
inputCopy
5 3
1 0 0 0 1
outputCopy
2
inputCopy
5 10
0 0 0 0 0
outputCopy
-1
inputCopy
10 3
0 0 1 1 0 1 0 0 0 1
outputCopy
3
Note
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.

题意:给你一段序列,0代表需要覆盖的地方,1代表起点,输入n和r,n代表长度,r代表覆盖半径,问你最少要多少个1才能覆盖完成,如果不可能输出-1

思路:先每个1可覆盖的地方算出来,最后从第一个点开始扩张,每次选最接近上一个终端+1的就可以了

#include<bits/stdc++.h>
#define LL long long
#define Max 1005
#define Mod 1e9+7
const LL mod=1e9+7;
const LL inf=0x3f3f3f3f;
using namespace std;
int n,r;
int a[Max];
struct node
{
    int st,en;
} vis[Max];//保存每个1可覆盖范围
bool cmp(node a,node b)
{
    if(a.st==b.st)
        return a.en>b.en;
    return a.st<b.st;
}//排序
int main()
{
    scanf("%d%d",&n,&r);
    int len=0;
    int mst=inf,men=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==1)
        {
            vis[len].st=i-r+1;
            if(vis[len].st<=0)
                vis[len].st=1;//如果超过范围把它变为1
            vis[len].en=i+r-1;
            if(vis[len].en>n)
                vis[len].en=n;//同理
            mst=min(mst,vis[len].st);//记录最小的左端点
            men=max(men,vis[len].en);//记录最大右端点
            len++;
        }
    }
    sort(vis,vis+len,cmp);
    if(mst>1 || len==0 || men<n)//没有1,或者不能全部覆盖输出-1
    {
        printf("-1\n");
        return 0;
    }
    int en=vis[0].en;
    vis[len].st=inf,vis[len].en=inf;
    int ans=1;
    if(en!=n)//如果第1条就可以全覆盖,直接输出
        for(int i=1; i<=len; i++)
        {
            while(vis[i].st<=en+1)
                i++;
            if(vis[i-1].st>en+1)//如果没有可以连接的范围,输出-1
            {
                printf("-1");
                return 0;
            }
            en=vis[i-1].en;
            ans++;
            if(en==n)
                break;
        }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Gee_Zer/article/details/89184271