Submission #404591

# Submission time Handle Problem Language Result Execution time Memory
404591 2021-05-14T17:15:45 Z rama_pang Bomb (IZhO17_bomb) C++17
100 / 100
512 ms 73972 KB
#include <bits/stdc++.h>
using namespace std;

int N, M;
int A[2505][2505];
int up[2505][2505];
int dn[2505][2505];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  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 - '0';
    }
  }

  // Solution:
  //
  // The upper bound on height and width is the minimum length
  // vertical and horizontal strip. Now, for a fixed width Y,
  // we want to determine the maximum height X. Note that,
  // if we have width Y, then for every maximal horizontal
  // strip, height is bounded by the minimum (top - bottom)
  // of the prefix and suffix of size Y.
  //
  // We only need to consider the prefix and suffix. Why?
  // For, after a prefix of size Y, let's move it to the right.
  // If the (top - bottom) decreased, that means that there is
  // a vertical strip which is shifted downwards, which means
  // it will occcupy a maximal horizontal strip -> we will compute
  // it later.
  //
  // Time complexity: O(N M).

  for (int j = 1; j <= M; j++) {
    for (int i = 1; i <= N; i++) {
      if (A[i][j]) {
        up[i][j] = 1 + up[i - 1][j];
      }
    }
    for (int i = N; i >= 1; i--) {
      if (A[i][j]) {
        dn[i][j] = 1 + dn[i + 1][j];
      }
    }
  }

  vector<int> ans(M + 2, N);
  for (int rep = 0; rep < 2; rep++) { // for prefix, then suffix
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= M; j++) if (A[i][j]) {
        ans[1] = min(ans[1], up[i][j] + dn[i][j] - 1);
      }
      for (int j = 1; j <= M; j++) if (A[i][j] && !A[i][j - 1]) {
        int jj = j;
        while (A[i][jj + 1]) jj += 1;

        int len = jj - j + 1;
        int minUp = N;
        int minDn = N;
        for (int k = 1; k <= len; k++) {
          minUp = min(minUp, up[i][j + k - 1]);
          minDn = min(minDn, dn[i][j + k - 1]);
          ans[k] = min(ans[k], minUp + minDn - 1);
        }

        ans[len + 1] = 0;
      }
    }
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j + j <= M; j++) {
        swap(A[i][j], A[i][M - j + 1]);
        swap(up[i][j], up[i][M - j + 1]);
        swap(dn[i][j], dn[i][M - j + 1]);
      }
    }
  }

  int answer = 0;
  for (int i = 1; i <= M; i++) {
    ans[i] = min(ans[i], ans[i - 1]);
    answer = max(answer, i * ans[i]);
  }

  cout << answer << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 14 ms 30412 KB Output is correct
4 Correct 14 ms 30420 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 2 ms 1100 KB Output is correct
18 Correct 1 ms 1228 KB Output is correct
19 Correct 2 ms 1484 KB Output is correct
20 Correct 2 ms 1484 KB Output is correct
21 Correct 1 ms 972 KB Output is correct
22 Correct 2 ms 1228 KB Output is correct
23 Correct 2 ms 1612 KB Output is correct
24 Correct 1 ms 1356 KB Output is correct
25 Correct 2 ms 1612 KB Output is correct
26 Correct 2 ms 1612 KB Output is correct
27 Correct 6 ms 4772 KB Output is correct
28 Correct 6 ms 5068 KB Output is correct
29 Correct 9 ms 6476 KB Output is correct
30 Correct 10 ms 7628 KB Output is correct
31 Correct 8 ms 6092 KB Output is correct
32 Correct 8 ms 6976 KB Output is correct
33 Correct 11 ms 8136 KB Output is correct
34 Correct 6 ms 5580 KB Output is correct
35 Correct 10 ms 8012 KB Output is correct
36 Correct 14 ms 8036 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 469 ms 73828 KB Output is correct
39 Correct 1 ms 460 KB Output is correct
40 Correct 57 ms 19908 KB Output is correct
41 Correct 1 ms 460 KB Output is correct
42 Correct 2 ms 1636 KB Output is correct
43 Correct 419 ms 73864 KB Output is correct
44 Correct 12 ms 8032 KB Output is correct
45 Correct 418 ms 73944 KB Output is correct
46 Correct 393 ms 73848 KB Output is correct
47 Correct 421 ms 73924 KB Output is correct
48 Correct 380 ms 73940 KB Output is correct
49 Correct 490 ms 73788 KB Output is correct
50 Correct 393 ms 73844 KB Output is correct
51 Correct 386 ms 73836 KB Output is correct
52 Correct 393 ms 73796 KB Output is correct
53 Correct 380 ms 73812 KB Output is correct
54 Correct 332 ms 73856 KB Output is correct
55 Correct 325 ms 73852 KB Output is correct
56 Correct 493 ms 73764 KB Output is correct
57 Correct 333 ms 73816 KB Output is correct
58 Correct 338 ms 73852 KB Output is correct
59 Correct 329 ms 73796 KB Output is correct
60 Correct 388 ms 73888 KB Output is correct
61 Correct 495 ms 73796 KB Output is correct
62 Correct 512 ms 73852 KB Output is correct
63 Correct 509 ms 73900 KB Output is correct
64 Correct 328 ms 73872 KB Output is correct
65 Correct 381 ms 73820 KB Output is correct
66 Correct 395 ms 73828 KB Output is correct
67 Correct 396 ms 73848 KB Output is correct
68 Correct 421 ms 73876 KB Output is correct
69 Correct 336 ms 73880 KB Output is correct
70 Correct 183 ms 59224 KB Output is correct
71 Correct 364 ms 73796 KB Output is correct
72 Correct 329 ms 73856 KB Output is correct
73 Correct 336 ms 73744 KB Output is correct
74 Correct 319 ms 73796 KB Output is correct
75 Correct 338 ms 73784 KB Output is correct
76 Correct 333 ms 73776 KB Output is correct
77 Correct 339 ms 73852 KB Output is correct
78 Correct 330 ms 73848 KB Output is correct
79 Correct 270 ms 73896 KB Output is correct
80 Correct 308 ms 73792 KB Output is correct
81 Correct 276 ms 73812 KB Output is correct
82 Correct 339 ms 73836 KB Output is correct
83 Correct 343 ms 73828 KB Output is correct
84 Correct 279 ms 73852 KB Output is correct
85 Correct 344 ms 73796 KB Output is correct
86 Correct 458 ms 73804 KB Output is correct
87 Correct 328 ms 73748 KB Output is correct
88 Correct 332 ms 73728 KB Output is correct
89 Correct 375 ms 73796 KB Output is correct
90 Correct 206 ms 59232 KB Output is correct
91 Correct 347 ms 73796 KB Output is correct
92 Correct 364 ms 73840 KB Output is correct
93 Correct 462 ms 73952 KB Output is correct
94 Correct 383 ms 73752 KB Output is correct
95 Correct 342 ms 73836 KB Output is correct
96 Correct 346 ms 73840 KB Output is correct
97 Correct 471 ms 73972 KB Output is correct
98 Correct 342 ms 73768 KB Output is correct
99 Correct 366 ms 73852 KB Output is correct
100 Correct 481 ms 73764 KB Output is correct