【题解】CF1066B

Solution

很显然是一道贪心的题目。

我们预处理得到每个点左边离它最近的加热炉的位置,然后可以用一个指针表示当前可照亮的点的位置,然后找到离它最远但可以照亮它。

后面的点的位置,然后一直这样暴力枚举下去,用 tot 进行记录,最后输出 tot 及答案。

但要注意在求的过程中判断无解情况。

Code

#include <cstdio>
using namespace std;
const int MAXN = 2005;
int n, r, a[MAXN];
int main() {
    scanf("%d %d", &n, &r);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]), a[i] = a[i] ? i : a[i - 1];
    for (int i = n + 1; i <= n + r; i++)
        a[i] = a[i - 1];
    int x = a[r] + r - 1, tot = 1;
    while (x < n && tot < MAXN)
        tot++, x = a[x + r] + r - 1;
    if (!a[r] || tot == MAXN) {
        printf("-1\n");
        return 0;
    }
    printf("%d\n", tot);
    return 0;
}
posted @   zhou_ziyi  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示