【题解】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 @ 2022-07-27 21:29  zhou_ziyi  阅读(26)  评论(0)    收藏  举报