Educational Codeforces Round 28 D. Monitor · 二分 + 前缀和

题解

题意:一个 n ∗ m n*m nm的显示器,如果 k ∗ k k*k kk的正方形里所有的像素都是损坏的,则显示器立刻损坏,然后再告诉你 q q q个点 ( x , y ) (x,y) (x,y)会在 t t t 时间时损坏,问显示器完全损坏的时间

范围很小,
二分时间上界,可以看出来求得是最小的损坏时间,
用前缀和暴力计算二维区间内损坏的点是否为k*k的正方形,

因为t可以取到0,为方便统计,时间都+1,最后再减去即可


Educational Codeforces Round 28 D. Monitor · 二分 + 前缀和_第1张图片


#include 
using namespace std;
typedef long long ll;
const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
int n, m, k, q;
int mp[N][N], sum[N][N];

bool legal(int t) {//时间上限
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (mp[i][j] && mp[i][j] <= t) sum[i][j] = 1;
            else sum[i][j] = 0;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
        }
    }
    for (int i = k; i <= n; ++i) {
        for (int j = k; j <= m; ++j) {
            ll tmp = sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k];
            if (tmp == k * k) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(0);

    cin >> n >> m >> k >> q;
    for (int i = 1, x, y; i <= q; ++i) {
        cin >> x >> y;
        cin >> mp[x][y];
        mp[x][y]++;
    }

    int l = 1, r = (int) 1e9+10;
    while (l < r) {
        int mid = l + r >> 1;
        if (legal(mid)) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }
    if (legal(r)) cout << r-1 << endl;
    else puts("-1");

    return 0;
}

你可能感兴趣的:(cf)