제출 #173202

#제출 시각아이디문제언어결과실행 시간메모리
173202VEGAnnBomb (IZhO17_bomb)C++14
47 / 100
1099 ms123364 KiB
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define MP make_pair
#define ft first
#define sd second
#define pii pair<int, int>
#define PB push_back
using namespace std;
using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2510;
const int oo = 2e9;
int n, m, ans, a[N][N], mx = oo, pref[N][N], an1;
short pr[N][N], best[N], nt[N][N], down[N][2], up[N][2];
int pl[N][N], vl[N][N];
stack<int> st;

bool ok(int ht, int wd){
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            pl[i][j] = 0;

    bool was = 1;
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++){
        if (i + ht - 1 > n || j + wd - 1 > m)
            break;
        int i1 = i + ht - 1, i2 = i - 1;
        int j1 = j + wd - 1, j2 = j - 1;
        int kol = pref[i1][j1] - pref[i2][j1] - pref[i1][j2] + pref[i2][j2];

        if (kol > 0) continue;

        was = 1;
        pl[i][j]++; pl[i][j + wd]--;
        pl[i + ht][j]--; pl[i + ht][j + wd]++;

    }

    if (!was) return 0;

    for (int i = 1; i <= n; i++){
        int cr = 0;
        for (int j = 1; j <= m; j++){
            cr += pl[i][j];
            vl[i][j] = cr;
        }
    }

    for (int j = 1; j <= m; j++){
        int cr = 0;
        for (int i = 1; i <= n; i++){
            cr += vl[i][j];
            if (a[i][j] == 1 && cr == 0)
                return 0;
        }
    }

    return 1;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("in.txt","r",stdin);

    ll sta = chrono::system_clock().now().time_since_epoch().count();

    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++){
        char c; cin >> c;
        a[i][j] = (c == '1' ? 1 : 0);

        pref[i][j] = (a[i][j] ^ 1) + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];

    }

    for (int j = 1; j <= m; j++){
        for (int i = 1; i <= n; ){
            if (a[i][j] == 0)
                i++;
            else {
                int i1 = i;
                while (i1 <= n && a[i1][j] == 1)
                    i1++;
                mx = min(mx, i1 - i);
                i = i1;
            }
        }
    }

    if (mx == oo){
        cout << n * m;
        return 0;
    }

    fill(best, best + n + 1, N);

    for (int i = 1; i <= n; i++){
        int pre = 0;
        for (int j = 1; j <= m; j++){
            if (a[i][j] == 0)
                pre = j;
            pr[i][j] = pre + 1;
        }

        pre = m + 1;
        for (int j = m; j > 0; j--){
            if (a[i][j] == 0)
                pre = j;
            nt[i][j] = pre - 1;
        }
    }

    for (int j = 1; j <= m; j++){
        for (int i = 1; i <= n; ){
            if (a[i][j] == 0)
                i++;
            else {
                int i1 = i;
                while (i1 <= n && a[i1][j] == 1)
                    i1++;

                while (sz(st)) st.pop();
                for (int ii = i; ii < i1; ii++){
                    while (sz(st) && pr[st.top()][j] <= pr[ii][j])
                        st.pop();
                    down[ii][0] = (sz(st) == 0 ? i : st.top() + 1);
                    st.push(ii);
                }

                while (sz(st)) st.pop();
                for (int ii = i; ii < i1; ii++){
                    while (sz(st) && nt[st.top()][j] >= nt[ii][j])
                        st.pop();
                    down[ii][1] = (sz(st) == 0 ? i : st.top() + 1);
                    st.push(ii);
                }

                while (sz(st)) st.pop();
                for (int ii = i1 - 1; ii >= i; ii--){
                    while (sz(st) && pr[st.top()][j] <= pr[ii][j])
                        st.pop();
                    up[ii][0] = (sz(st) == 0 ? i1 - 1 : st.top() - 1);
                    st.push(ii);
                }

                while (sz(st)) st.pop();
                for (int ii = i1 - 1; ii >= i; ii--){
                    while (sz(st) && nt[st.top()][j] >= nt[ii][j])
                        st.pop();
                    up[ii][1] = (sz(st) == 0 ? i1 - 1 : st.top() - 1);
                    st.push(ii);
                }

                i = i1;
            }
        }

        for (int i = 1; i <= n; i++){
            if (a[i][j] == 0) continue;
            int l = max(down[i][0], down[i][1]);
            int r = min(up[i][0], up[i][1]);
            int cur = r - l + 1;
            best[cur] = min(best[cur], short(nt[i][j] - pr[i][j] + 1));
        }
    }

    best[n + 1] = N;
    for (int i = n; i > 0; i--) {
        best[i] = min(best[i + 1], best[i]);
        if (best[i] < N && i <= mx)
            ans = max(ans, int(best[i]) * i);
    }

    bool full = 1;
    int wd = m;
    for (int ht = 1; ht <= n; ht++){
        ll tim = chrono::system_clock().now().time_since_epoch().count();;
        if (tim - sta > ll(1e9) - ll(3e8)) {
            full = 0;
            break;
        }
        while (wd > 0 && !ok(ht, wd))
            wd--;
        if (wd == 0)
            break;
        if (n * wd <= an1)
            break;
        an1 = max(an1, ht * wd);
    }

//    if (!full) return -1;

    if (full)
        cout << an1;
    else cout << max(ans, an1);

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...