CodeForces - 1066 B Heaters

本文介绍了一道CodeForces上的题目B Heaters。问题涉及到使用热水器为房间提供热水,其中热水器覆盖范围为[i-r+1,i+r-1]。目标是最小化热水器数量以覆盖所有房间。题目限制1<=n,r<=1e3,且0<=ai<=1。解题策略是从后往前扫描,判断热水器是否能覆盖前一个热水器的左区间。如果不能覆盖,则可能需要增加热水器,否则输出-1表示无法覆盖所有房间。作者反思自己太久没做限时训练,导致表现不佳。" 90048343,8515991,大数据算法解析:Bloom Filter、SkipList与LSM树,"['大数据', '数据结构', '算法', '并行编程', '存储系统']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B Heaters
题 意:一个大小为n的数组代表n个房间,数组元素为1代表这个房间又一个热水器,位置为i的热水器,可以热给范围为[i-r+1,i+r-1]范围的房间热水,问最少需要多少热水器,可以给所有房间热水。
数据范围:
1 &lt; = n , r &lt; = 1 e 3 1&lt;=n,r&lt;=1e3 1<=n,r<=1e3
0 &lt; = a i &lt; = 1 0&lt;=ai&lt;=1 0<=ai<=1
输入样例:

6 2
0 1 1 0 0 1
5 10
0 0 0 0 0

输出样例:

3
-1

思 路:每次从后往前扫,看看有没有那个header的左区间小于last。如果有就可以继续,如果没有且没有推出循环则直接输出-1.

收 获:很就没有做限时训练,这次的限时训练效果很差。(lll¬ω¬),太懒了。暴力也得要有姿势的。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
#define IN freopen("input.txt","r",stdin)
#define mst(x,y) memset(x,y,sizeof(x));
#define debug(x) cout<< #x <<" = "<< (x) <<endl;
#define min(x,y) x>y?y:x
#define max(x,y) x>y?x:y
using namespace std;
typedef long long ll;
typedef pair<ll,ll> lp;
typedef pair<int,int> P;
typedef unsigned long long ull;
const int mod = 1e6+3;
const int INF = 0x3f3f3f3f;
const int LINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e3+5;
int n,r;
int a[maxn];
int main() {
    //IN;
    scanf("%d%d",&n,&r);
    for(int i=1; i<=n; i++)scanf("%d",&a[i]);
    int last = 0,num = 0;
    bool flag;
    int pos;
    while(last<n) {
        flag = false;
        int pp = max(last-r+1,0);
        for(int i=n; i>pp; i--) {
            if(a[i]) {  
                if(i-r<=last) {
                    pos = i;
                    flag = true;
                    break;
                }
            }
        }
        if(!flag) {
            printf("-1\n");
            return 0;
        }
        last = pos+r-1;
        num++;
    }
    printf("%d\n",num);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值