codeforces Heaters

题目: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.

题目要求的是最小电量的heater的数量,那我们对于每一个点都处理出他前面有哪个可以让他变热,以及后面最远哪个可以让它变热,最后扫一遍就行了。由于n和r都小于1000,直接暴力。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int late[N];
bool has[N];
int pre[N];
int main()
{
    int n, r;
    scanf("%d%d", &n, &r);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &has[i]);
        late[i] = has[i]? i : 0;
        if (has[i])
        {
            pre[i] = i;
            for (int j = 1; j < r; j++)
            {
                if (i - j <= 0)
                    break;
                late[i - j] = i;
            }
            for (int j = 1; j < r; j++)
            {
                if (i + j > n)
                    break;
                pre[i + j] = i;
            }
        }
    }
    int cnt = 0;
    int now = 1;
    bool can = 1;
    while (now <= n)
    {
        if (!late[now])
        {
            if (!pre[now])
            {
                can = 0;
                break;
            }
            cnt++;
            now = pre[now];
            now += r;
            continue;
        }
        now = late[now];
        cnt++;
        now += r;
    }
    if (!can)
    {
        puts("-1");
        return 0;
    }
    printf("%d\n", cnt);
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值