Submission #1037226

#TimeUsernameProblemLanguageResultExecution timeMemory
1037226AlfraganusBomb (IZhO17_bomb)C++17
29 / 100
1097 ms131072 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define str string
#define fastio ios::sync_with_stdio(0), cin.tie(0);
#define fs first
#define ss second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define len(x) x.size()

#define print(a)          \
    for (auto &x : a)     \
        cout << x << " "; \
    cout << endl;

#define printmp(a)    \
    for (auto &x : a) \
        cout << x.fs << " " << x.ss << endl;

const int mod = 1e9 + 7;

void solve(){
    int n, m;
    cin >> n >> m;
    vector<vector<char>> a(n, vector<char> (m));
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < m; j ++)
            cin >> a[i][j];
    if(n <= 40 and m <= 40){
        int ans = 0;
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++){
                vector<vector<char>> b = a;
                for(int i1 = 0; i1 <= n - i; i1 ++){
                    for(int i2 = 0; i2 <= m - j; i2 ++){
                        if(b[i1][i2] != '0'){
                            bool flag = 1;
                            for(int x = 0; x < i; x ++){
                                for(int y = 0; y < j; y ++){
                                    if(b[i1 + x][i2 + y] == '0')
                                        flag = false;
                                }
                            }
                            if(flag){
                                for(int x = 0; x < i; x ++){
                                    for(int y = 0; y < j; y ++){
                                        b[i1 + x][i2 + y] = '2';
                                    }
                                }
                            }
                        }
                    }
                }
                int cnt = 0;
                for(int i = 0; i < n; i ++){
                    for(int j = 0; j < m; j ++){
                        cnt += b[i][j] == '1';
                    }
                }
                if(cnt == 0)
                    ans = max(ans, i * j);
            }
        }
        cout << ans;
        return;
    }
    vector<vector<int>> pref(n + 1, vector<int> (m + 1));
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < m; j ++)
            pref[i + 1][j + 1] = pref[i][j + 1] + pref[i + 1][j] - pref[i][j] + (a[i][j] == '0');
    vector<vector<array<int, 2>>> dp(n, vector<array<int, 2>> (m, {0, 0}));
    int ans = 1;
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= m; j ++){
            bool flag = 1;
            for(int x = 0; x < n; x ++){
                for(int y = 0; y < m; y ++){
                    if(x + i <= n and y + j <= m and pref[x + i][y + j] - pref[x + i][y] - pref[x][y + j] + pref[x][y] == 0)
                        dp[x][y] = {i, j};
                    else{
                        dp[x][y] = {0, 0};
                        if(y > 0 and dp[x][y - 1][1] > 1)
                            dp[x][y] = {dp[x][y - 1][0], dp[x][y - 1][1] - 1};
                        else if(x > 0 and dp[x - 1][y][0] > 1)
                            dp[x][y] = {dp[x - 1][y][0] - 1, dp[x - 1][y][1]};
                    }
                    if(a[x][y] == '1' and dp[x][y][0] == 0 and dp[x][y][1] == 0){
                        flag = 0;
                        break;
                    }
                }
                if(!flag)
                    break;
            }
            if(flag)
                ans = max(ans, i * j);
            else
                break;
        }
    }
    cout << ans;
}

signed main()
{
    fastio;
    #ifdef Javohir
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
        cout << endl;
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...