Submission #854579

#TimeUsernameProblemLanguageResultExecution timeMemory
854579The_SamuraiBomb (IZhO17_bomb)C++17
29 / 100
1102 ms32380 KiB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9, N = 1e6 + 5;

int main() {
    cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("game.in", "r", stdin);
//    freopen("game.out", "w", stdout);
#endif

    int n, m;
    cin >> n >> m;
    vector<vector<char>> a(n + 1, vector<char>(m + 1));
    vector<vector<int>> pref(n + 1, vector<int>(m + 1));
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
        pref[i][j] = pref[i][j - 1] + pref[i - 1][j] - pref[i - 1][j - 1] + (int) (a[i][j] == '1');
    }
    auto get_sum = [&](int x1, int y1, int x2, int y2) {
        return pref[x2][y2] - pref[x2][y1 - 1] - pref[x1 - 1][y2] + pref[x1 - 1][y1 - 1];
    };
    auto check = [&](int h, int w) {
        vector<vector<bool>> vis(n + 1, vector<bool>(m + 1));
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (a[i][j] == '0') continue;
                if (i + h - 1 <= n and j + w - 1 <= m and get_sum(i, j, i + h - 1, j + w - 1) == h * w) {
                    for (int x = i; x < i + h; x++) {
                        for (int y = j; y < j + w; y++) {
                            vis[x][y] = true;
                        }
                    }
                }
                if (!vis[i][j]) return false;
            }
        }
        return true;
    };
    vector<int> v(n + 1, -1);
    auto get = [&](int h) {
        if (v[h] != -1) return v[h];
        int lx = 1, rx = m, best = 0;
        while (lx <= rx) {
            int mid = (lx + rx) >> 1;
            if (check(h, mid)) {
                best = mid;
                lx = mid + 1;
            } else {
                rx = mid - 1;
            }
        }
        return v[h] = best;
    };
    get(1);
    for (int i = 2; i <= n; i++) {
        if (get(i) * i < get(i - 1) * (i - 1)) {
            cout << get(i - 1) * (i - 1);
            return 0;
        }
    }
    cout << get(n) * n;
}
#Verdict Execution timeMemoryGrader output
Fetching results...