Submission #945104

# Submission time Handle Problem Language Result Execution time Memory
945104 2024-03-13T12:03:45 Z atom Bomb (IZhO17_bomb) C++17
100 / 100
385 ms 80144 KB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
 
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

const int N = 2505;

int a[N][N], lt[N][N], rt[N][N];
int w[N];

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    #ifdef JASPER
        freopen("in1", "r", stdin);
    #endif
 
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            char x; cin >> x;
            a[i][j] = x - '0';
        }
    }
 
    // for (int i = 1; i <= n; ++i) 
    //     for (int j = 1; j <= m; ++j)
    //         cout << a[i][j] << " \n"[j == m]; 
 
    //sub1: shortest consecutive 1ss
    if (n == 1 || m == 1) {
        if (n == 1) {
            int ans = 1e9;
            for (int i = 1; i <= m; ++i) {
                if (a[1][i] == 0) continue;
                int cur = 0;
                while (i <= m && a[1][i]) { 
                    ++cur;
                    ++i;
                }
                ans = min(ans, cur);
            }
            cout << ans << "\n";
            return 0;
        }
        if (m == 1) {
            int ans = 1e9;
            for (int i = 1; i <= n; ++i) {
                if (a[i][1] == 0) continue;
                int cur = 0;
                while (i <= n && a[i][1]) { 
                    ++cur;
                    ++i;
                }
                ans = min(ans, cur);
            }
            cout << ans << "\n";
            return 0;
        }
    }
 
    if (n <= 100 && m <= 100) {
        vector <vector <int>> prf(n + 5, vector <int> (m + 5, 0));
        int earth = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                earth += (a[i][j] == 0);
                prf[i][j] = prf[i][j - 1] + prf[i - 1][j] - prf[i - 1][j - 1] + a[i][j];
            }
        }
 
        int ans = 0;
        for (int x = 1; x <= n; ++x) {
            for (int y = 1; y <= m; ++y) {
                if (x * y <= ans) continue;
 
                // prf(l)++, prf(r + 1)--;
                vector <vector <int>> dmg(n + 2, vector <int> (m + 2, 0));
 
                for (int i = 1; i + x - 1 <= n; ++i) {
                    for (int j = 1; j + y - 1 <= m; ++j) {
                        int I = i + x - 1, J = j + y - 1;
                        if (prf[I][J] + prf[i - 1][j - 1] - prf[I][j - 1] - prf[i - 1][J] == x * y) {
                            dmg[i][j] += 1;
                            dmg[I + 1][j] -= 1;
                            dmg[i][J + 1] -= 1;
                            dmg[I + 1][J + 1] += 1;
                        }
                    }
                }
 
                for (int i = 1; i <= n; ++i)
                    for (int j = 1; j <= m; ++j)
                        dmg[i][j] += dmg[i - 1][j] + dmg[i][j - 1] - dmg[i - 1][j - 1];
 
                bool valid = 1;
                for (int i = 1; i <= n; ++i) {
                    for (int j = 1; j <= m; ++j) {
                        if (a[i][j] && !dmg[i][j])
                            valid = 0;
                    }
                }
                if (valid) ans = max(ans, x * y);
            }
        }
 
        cout << ans << "\n";
        return 0;
    }
    
 
    for (int i = 1; i <= n; ++i) w[i] = m;
    // two pointer to optimize
    int min_h = 1e9;// minimum vertical strip of 1s;
 
    for (int _i = 0; _i <= 1; _i++) {
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j)
                lt[i][j] = a[i][j]? (lt[i][j - 1] + 1) : 0;
 
            for (int j = m; j >= 1; --j)
                rt[i][j] = a[i][j]? (rt[i][j + 1] + 1) : 0;
 
            for (int j = 1; j <= m; ++j)
                if (a[i][j]) w[1] = min(w[1], rt[i][j] + lt[i][j] - 1);
        }
        // find feasible height
        for (int j = 1; j <= m; ++j) {
            int k = 0; // length of consecutive 1s of current column
            int l = 1e9, r = 1e9;
            for (int i = 1; i <= n; ++i) {
                if (a[i][j]) {
                    ++k;
                    l = min(l, lt[i][j]);
                    r = min(r, rt[i][j]);
                    w[k] = min(w[k], r + l - 1);
                }
                else {
                    if (k) min_h = min(min_h, k);
                    l = r = 1e9;
                    k = 0;
                }
            }
        }
 
        for (int i = 1; i <= n / 2; ++i) {
            for (int j = 1; j <= m; ++j)
                swap(a[i][j], a[n - i + 1][j]);
        }
 
    }
 
    int ans = 0;
    for (int h = 1; h <= min(min_h, n); ++h) {
        ans = max(ans, w[h] * h);
        w[h + 1] = min(w[h + 1], w[h]); // width is bounded by the minimum horizontal strip 
    }
 
    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 6 ms 25692 KB Output is correct
4 Correct 3 ms 25692 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 86 ms 4604 KB Output is correct
18 Correct 76 ms 4444 KB Output is correct
19 Correct 207 ms 4616 KB Output is correct
20 Correct 231 ms 4620 KB Output is correct
21 Correct 87 ms 4588 KB Output is correct
22 Correct 210 ms 4440 KB Output is correct
23 Correct 364 ms 4444 KB Output is correct
24 Correct 141 ms 4440 KB Output is correct
25 Correct 381 ms 4444 KB Output is correct
26 Correct 385 ms 4636 KB Output is correct
27 Correct 4 ms 11100 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 5 ms 15708 KB Output is correct
30 Correct 6 ms 14172 KB Output is correct
31 Correct 5 ms 14220 KB Output is correct
32 Correct 5 ms 13912 KB Output is correct
33 Correct 7 ms 20568 KB Output is correct
34 Correct 4 ms 16728 KB Output is correct
35 Correct 6 ms 21084 KB Output is correct
36 Correct 9 ms 14684 KB Output is correct
37 Correct 1 ms 2396 KB Output is correct
38 Correct 371 ms 79948 KB Output is correct
39 Correct 1 ms 2396 KB Output is correct
40 Correct 41 ms 26108 KB Output is correct
41 Correct 1 ms 2396 KB Output is correct
42 Correct 334 ms 4644 KB Output is correct
43 Correct 272 ms 74004 KB Output is correct
44 Correct 8 ms 20056 KB Output is correct
45 Correct 278 ms 74076 KB Output is correct
46 Correct 251 ms 73952 KB Output is correct
47 Correct 265 ms 74068 KB Output is correct
48 Correct 242 ms 73964 KB Output is correct
49 Correct 356 ms 74028 KB Output is correct
50 Correct 259 ms 74192 KB Output is correct
51 Correct 260 ms 74068 KB Output is correct
52 Correct 247 ms 74080 KB Output is correct
53 Correct 252 ms 74064 KB Output is correct
54 Correct 199 ms 74080 KB Output is correct
55 Correct 206 ms 73956 KB Output is correct
56 Correct 323 ms 80144 KB Output is correct
57 Correct 204 ms 73944 KB Output is correct
58 Correct 205 ms 74336 KB Output is correct
59 Correct 217 ms 73968 KB Output is correct
60 Correct 258 ms 73956 KB Output is correct
61 Correct 362 ms 73936 KB Output is correct
62 Correct 361 ms 73928 KB Output is correct
63 Correct 371 ms 74072 KB Output is correct
64 Correct 223 ms 74028 KB Output is correct
65 Correct 257 ms 74204 KB Output is correct
66 Correct 263 ms 74068 KB Output is correct
67 Correct 276 ms 74068 KB Output is correct
68 Correct 291 ms 73944 KB Output is correct
69 Correct 210 ms 74064 KB Output is correct
70 Correct 110 ms 64456 KB Output is correct
71 Correct 168 ms 73812 KB Output is correct
72 Correct 191 ms 74032 KB Output is correct
73 Correct 219 ms 73808 KB Output is correct
74 Correct 194 ms 73944 KB Output is correct
75 Correct 197 ms 74032 KB Output is correct
76 Correct 216 ms 73808 KB Output is correct
77 Correct 203 ms 73808 KB Output is correct
78 Correct 196 ms 73960 KB Output is correct
79 Correct 144 ms 73964 KB Output is correct
80 Correct 145 ms 74276 KB Output is correct
81 Correct 146 ms 73808 KB Output is correct
82 Correct 198 ms 74032 KB Output is correct
83 Correct 201 ms 74120 KB Output is correct
84 Correct 141 ms 73956 KB Output is correct
85 Correct 210 ms 74032 KB Output is correct
86 Correct 353 ms 74108 KB Output is correct
87 Correct 205 ms 73940 KB Output is correct
88 Correct 200 ms 74204 KB Output is correct
89 Correct 245 ms 74028 KB Output is correct
90 Correct 130 ms 63208 KB Output is correct
91 Correct 217 ms 73808 KB Output is correct
92 Correct 236 ms 74100 KB Output is correct
93 Correct 325 ms 73808 KB Output is correct
94 Correct 244 ms 74068 KB Output is correct
95 Correct 201 ms 73952 KB Output is correct
96 Correct 199 ms 74028 KB Output is correct
97 Correct 348 ms 74116 KB Output is correct
98 Correct 197 ms 73812 KB Output is correct
99 Correct 247 ms 74032 KB Output is correct
100 Correct 330 ms 74032 KB Output is correct