bzoj 1088 [SCOI2005]扫雷Mine

题面

题目传送门

解法

显然答案不超过2

直接暴力枚举第一格到底是否有雷即可,后面的状态自然就可以确定了

时间复杂度:\(O(n)\)

代码

#include <bits/stdc++.h>
#define N 10010
using namespace std;
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
int n, a[N], b[N], c[N];
int check(int t) {
    for (int i = 1; i <= n; i++) b[i] = a[i];
    c[1] = t; b[1] -= t, b[2] -= t;
    if (b[1] < 0 || b[2] < 0) return 0;
    for (int i = 2; i <= n; i++) {
        int mn = 10;
        for (int j = i - 1; j <= i + 1 && j <= n; j++)
            mn = min(mn, b[j]);
        c[i] = (mn != 0);
        for (int j = i - 1; j <= i + 1 && j <= n; j++)
            b[j] -= c[i];
    }
    for (int i = 1; i <= n; i++)
        if (b[i]) return false;
    return true;
}
int main() {
    read(n);
    for (int i = 1; i <= n; i++) read(a[i]);
    cout << check(0) + check(1) << "\n";
    return 0;
}
posted @ 2018-08-14 23:23  谜のNOIP  阅读(...)  评论(... 编辑 收藏