#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#ifdef JASPER
freopen("in1", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector <vector <int>> a(n + 5, vector <int> (m + 5, 0));
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;
}
}
// sub3
// if (n <= 450 && m <= 450) {
// 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;
// }
// two pointer to optimize
vector <vector <int>> lt(n + 5, vector <int> (m + 5, 0)), rt(n + 5, vector <int> (m + 5, 0));
vector <int> w(n + 5, 1e9); // ans(i) : W when H = i
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_h; ++h) {
w[h + 1] = min(w[h + 1], w[h]); // width is bounded by the minimum horizontal strip
ans = max(ans, w[h] * h);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
424 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1628 KB |
Output is correct |
29 |
Correct |
4 ms |
1884 KB |
Output is correct |
30 |
Correct |
5 ms |
2652 KB |
Output is correct |
31 |
Correct |
4 ms |
2140 KB |
Output is correct |
32 |
Correct |
4 ms |
2140 KB |
Output is correct |
33 |
Correct |
6 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
35 |
Correct |
5 ms |
2908 KB |
Output is correct |
36 |
Correct |
8 ms |
2904 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Execution timed out |
2244 ms |
131072 KB |
Time limit exceeded |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
36 ms |
9560 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
407 ms |
74384 KB |
Output is correct |
44 |
Correct |
7 ms |
2904 KB |
Output is correct |
45 |
Correct |
388 ms |
74580 KB |
Output is correct |
46 |
Correct |
360 ms |
74256 KB |
Output is correct |
47 |
Correct |
397 ms |
74240 KB |
Output is correct |
48 |
Correct |
400 ms |
74240 KB |
Output is correct |
49 |
Correct |
484 ms |
74388 KB |
Output is correct |
50 |
Correct |
371 ms |
74384 KB |
Output is correct |
51 |
Correct |
367 ms |
74256 KB |
Output is correct |
52 |
Correct |
371 ms |
74384 KB |
Output is correct |
53 |
Correct |
368 ms |
74244 KB |
Output is correct |
54 |
Correct |
292 ms |
74320 KB |
Output is correct |
55 |
Correct |
281 ms |
74256 KB |
Output is correct |
56 |
Execution timed out |
2683 ms |
131072 KB |
Time limit exceeded |
57 |
Correct |
271 ms |
74320 KB |
Output is correct |
58 |
Correct |
291 ms |
74384 KB |
Output is correct |
59 |
Correct |
275 ms |
74384 KB |
Output is correct |
60 |
Correct |
345 ms |
74236 KB |
Output is correct |
61 |
Correct |
485 ms |
74636 KB |
Output is correct |
62 |
Correct |
495 ms |
74492 KB |
Output is correct |
63 |
Correct |
487 ms |
74328 KB |
Output is correct |
64 |
Correct |
266 ms |
74384 KB |
Output is correct |
65 |
Correct |
349 ms |
74380 KB |
Output is correct |
66 |
Correct |
355 ms |
74512 KB |
Output is correct |
67 |
Correct |
375 ms |
74328 KB |
Output is correct |
68 |
Correct |
432 ms |
74384 KB |
Output is correct |
69 |
Correct |
312 ms |
74328 KB |
Output is correct |
70 |
Correct |
142 ms |
47856 KB |
Output is correct |
71 |
Correct |
243 ms |
74324 KB |
Output is correct |
72 |
Correct |
274 ms |
74384 KB |
Output is correct |
73 |
Correct |
296 ms |
74384 KB |
Output is correct |
74 |
Correct |
280 ms |
74384 KB |
Output is correct |
75 |
Correct |
287 ms |
74380 KB |
Output is correct |
76 |
Correct |
289 ms |
74380 KB |
Output is correct |
77 |
Correct |
300 ms |
74580 KB |
Output is correct |
78 |
Correct |
291 ms |
74320 KB |
Output is correct |
79 |
Correct |
197 ms |
74380 KB |
Output is correct |
80 |
Correct |
220 ms |
74244 KB |
Output is correct |
81 |
Correct |
196 ms |
74384 KB |
Output is correct |
82 |
Correct |
306 ms |
74380 KB |
Output is correct |
83 |
Correct |
295 ms |
74324 KB |
Output is correct |
84 |
Correct |
195 ms |
74320 KB |
Output is correct |
85 |
Correct |
309 ms |
74380 KB |
Output is correct |
86 |
Correct |
466 ms |
74576 KB |
Output is correct |
87 |
Correct |
289 ms |
74384 KB |
Output is correct |
88 |
Correct |
301 ms |
74256 KB |
Output is correct |
89 |
Correct |
340 ms |
74576 KB |
Output is correct |
90 |
Correct |
171 ms |
47852 KB |
Output is correct |
91 |
Correct |
309 ms |
74384 KB |
Output is correct |
92 |
Correct |
340 ms |
74384 KB |
Output is correct |
93 |
Correct |
459 ms |
74236 KB |
Output is correct |
94 |
Correct |
337 ms |
74384 KB |
Output is correct |
95 |
Correct |
307 ms |
74384 KB |
Output is correct |
96 |
Correct |
291 ms |
74384 KB |
Output is correct |
97 |
Correct |
473 ms |
74384 KB |
Output is correct |
98 |
Correct |
300 ms |
74240 KB |
Output is correct |
99 |
Correct |
330 ms |
74388 KB |
Output is correct |
100 |
Correct |
448 ms |
74388 KB |
Output is correct |