#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using ll = long long;
#define vt vector
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define pb push_back
#define endl '\n'
#ifndef LOCAL
#define cerr if (0) std::cerr
#endif
/*
N^3: find max width for each given height
*/
int rs, cs;
vt<vt<bool>> grid;
vt<vt<int>> sfx;
int sum(int r1, int c1, int r2, int c2) {
r2 = min(r2, rs - 1);
c2 = min(c2, cs - 1);
return sfx[r1][c1] - sfx[r1][c2 + 1] - sfx[r2 + 1][c1] + sfx[r2 + 1][c2 + 1];
}
bool check(int w, int h) {
vt<vt<int>> pfx(rs, vt<int>(cs));
auto is_covered = [&] (int r, int c) {
return (pfx[r][c] - (r - h >= 0 ? pfx[r - h][c] : 0) - (c - w >= 0 ? pfx[r][c - w] : 0) + (r - h >= 0 && c - w >= 0 ? pfx[r - h][c - w] : 0)) > 0;
};
auto add = [&] (int r, int c) {
pfx[r][c] += (r ? pfx[r - 1][c] : 0) + (c ? pfx[r][c - 1] : 0) - (r && c ? pfx[r - 1][c - 1] : 0);
};
F0R (r, rs - 1) {
F0R (c, cs - 1) {
if (sum(r, c, r + h - 1, c + w - 1) == w * h) {
pfx[r][c]++;
}
add(r, c);
if (grid[r][c] == 1) {
if (!is_covered(r, c)) return false;
}
}
}
return true;
}
int solve(int mnh, int mxh, int mnw, int mxw) {
if (mxh < mnh || mxw < mnw) return 0;
int h = (mnh + mxh) / 2;
int lo = mnw - 1, hi = mxw + 1;
while (hi - lo > 1) {
int w = (lo + hi) / 2;
if (check(w, h)) lo = w;
else hi = w;
}
return max({h * lo, solve(mnh, h - 1, lo, mxw), solve(h + 1, mxh, mnw, lo)});
}
main() {
cin.tie(0)->sync_with_stdio();
cin >> rs >> cs;
rs += 2, cs += 2;
grid.resize(rs, vt<bool>(cs));
sfx.resize(rs + 1, vt<int>(cs + 1));
FOR (r, 1, rs - 1) {
FOR (c, 1, cs - 1) {
char ch; cin >> ch;
grid[r][c] = sfx[r][c] = ch - '0';
}
}
ROF (r, 0, rs) ROF (c, 0, cs) sfx[r][c] += sfx[r][c + 1];
ROF (r, 0, rs) ROF (c, 0, cs) sfx[r][c] += sfx[r + 1][c];
int mxw = cs, mxh = rs;
F0R (r, rs) {
int streak = 0;
F0R (c, cs) {
if (grid[r][c]) streak++;
else if (streak) mxw = min(mxw, streak), streak = 0;
}
}
F0R (c, cs) {
int streak = 0;
F0R (r, rs) {
if (grid[r][c]) streak++;
else if (streak) mxh = min(mxh, streak), streak = 0;
}
}
cerr << mxw << " " << mxh << endl;
cout << solve(1, mxh, 1, mxw) << endl;
}
Compilation message
bomb.cpp:74:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
74 | main() {
| ^~~~
bomb.cpp: In function 'int main()':
bomb.cpp:84:36: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
84 | grid[r][c] = sfx[r][c] = ch - '0';
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
860 KB |
Output is correct |
4 |
Correct |
5 ms |
884 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 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 |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
444 KB |
Output is correct |
21 |
Correct |
0 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
440 KB |
Output is correct |
23 |
Correct |
2 ms |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
1180 KB |
Output is correct |
28 |
Correct |
8 ms |
1436 KB |
Output is correct |
29 |
Correct |
107 ms |
1728 KB |
Output is correct |
30 |
Correct |
126 ms |
2292 KB |
Output is correct |
31 |
Correct |
68 ms |
2164 KB |
Output is correct |
32 |
Correct |
91 ms |
1892 KB |
Output is correct |
33 |
Correct |
163 ms |
2320 KB |
Output is correct |
34 |
Correct |
7 ms |
1220 KB |
Output is correct |
35 |
Correct |
37 ms |
2316 KB |
Output is correct |
36 |
Correct |
78 ms |
2416 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Execution timed out |
1100 ms |
56696 KB |
Time limit exceeded |
39 |
Correct |
1 ms |
344 KB |
Output is correct |
40 |
Execution timed out |
1092 ms |
7484 KB |
Time limit exceeded |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
5 ms |
348 KB |
Output is correct |
43 |
Execution timed out |
1094 ms |
56604 KB |
Time limit exceeded |
44 |
Correct |
491 ms |
2552 KB |
Output is correct |
45 |
Execution timed out |
1016 ms |
56704 KB |
Time limit exceeded |
46 |
Execution timed out |
1072 ms |
56692 KB |
Time limit exceeded |
47 |
Execution timed out |
1060 ms |
56788 KB |
Time limit exceeded |
48 |
Execution timed out |
1044 ms |
56728 KB |
Time limit exceeded |
49 |
Correct |
566 ms |
56696 KB |
Output is correct |
50 |
Execution timed out |
1060 ms |
56860 KB |
Time limit exceeded |
51 |
Execution timed out |
1067 ms |
56744 KB |
Time limit exceeded |
52 |
Execution timed out |
1038 ms |
56800 KB |
Time limit exceeded |
53 |
Execution timed out |
1024 ms |
56744 KB |
Time limit exceeded |
54 |
Execution timed out |
1014 ms |
56752 KB |
Time limit exceeded |
55 |
Execution timed out |
1071 ms |
56792 KB |
Time limit exceeded |
56 |
Execution timed out |
1028 ms |
56728 KB |
Time limit exceeded |
57 |
Execution timed out |
1063 ms |
56828 KB |
Time limit exceeded |
58 |
Execution timed out |
1042 ms |
56696 KB |
Time limit exceeded |
59 |
Execution timed out |
1074 ms |
56656 KB |
Time limit exceeded |
60 |
Execution timed out |
1065 ms |
56772 KB |
Time limit exceeded |
61 |
Execution timed out |
1065 ms |
56696 KB |
Time limit exceeded |
62 |
Execution timed out |
1066 ms |
56816 KB |
Time limit exceeded |
63 |
Execution timed out |
1018 ms |
56952 KB |
Time limit exceeded |
64 |
Execution timed out |
1076 ms |
56700 KB |
Time limit exceeded |
65 |
Execution timed out |
1069 ms |
56708 KB |
Time limit exceeded |
66 |
Execution timed out |
1060 ms |
56620 KB |
Time limit exceeded |
67 |
Execution timed out |
1044 ms |
56772 KB |
Time limit exceeded |
68 |
Execution timed out |
1022 ms |
56984 KB |
Time limit exceeded |
69 |
Execution timed out |
1063 ms |
56700 KB |
Time limit exceeded |
70 |
Execution timed out |
1071 ms |
36576 KB |
Time limit exceeded |
71 |
Execution timed out |
1105 ms |
56736 KB |
Time limit exceeded |
72 |
Execution timed out |
1096 ms |
56620 KB |
Time limit exceeded |
73 |
Execution timed out |
1036 ms |
56724 KB |
Time limit exceeded |
74 |
Execution timed out |
1030 ms |
56832 KB |
Time limit exceeded |
75 |
Execution timed out |
1035 ms |
56828 KB |
Time limit exceeded |
76 |
Execution timed out |
1068 ms |
56664 KB |
Time limit exceeded |
77 |
Execution timed out |
1040 ms |
56744 KB |
Time limit exceeded |
78 |
Execution timed out |
1069 ms |
56740 KB |
Time limit exceeded |
79 |
Execution timed out |
1055 ms |
56748 KB |
Time limit exceeded |
80 |
Execution timed out |
1036 ms |
56768 KB |
Time limit exceeded |
81 |
Execution timed out |
1068 ms |
56756 KB |
Time limit exceeded |
82 |
Execution timed out |
1031 ms |
56748 KB |
Time limit exceeded |
83 |
Execution timed out |
1051 ms |
56744 KB |
Time limit exceeded |
84 |
Execution timed out |
1044 ms |
56916 KB |
Time limit exceeded |
85 |
Execution timed out |
1058 ms |
56788 KB |
Time limit exceeded |
86 |
Execution timed out |
1028 ms |
56748 KB |
Time limit exceeded |
87 |
Execution timed out |
1070 ms |
56748 KB |
Time limit exceeded |
88 |
Execution timed out |
1059 ms |
56968 KB |
Time limit exceeded |
89 |
Execution timed out |
1035 ms |
56748 KB |
Time limit exceeded |
90 |
Execution timed out |
1050 ms |
36576 KB |
Time limit exceeded |
91 |
Execution timed out |
1054 ms |
56788 KB |
Time limit exceeded |
92 |
Execution timed out |
1033 ms |
56784 KB |
Time limit exceeded |
93 |
Execution timed out |
1029 ms |
56856 KB |
Time limit exceeded |
94 |
Execution timed out |
1036 ms |
56748 KB |
Time limit exceeded |
95 |
Execution timed out |
1053 ms |
56712 KB |
Time limit exceeded |
96 |
Execution timed out |
1031 ms |
56704 KB |
Time limit exceeded |
97 |
Execution timed out |
1018 ms |
56904 KB |
Time limit exceeded |
98 |
Execution timed out |
1028 ms |
56932 KB |
Time limit exceeded |
99 |
Execution timed out |
1055 ms |
56828 KB |
Time limit exceeded |
100 |
Execution timed out |
1047 ms |
56820 KB |
Time limit exceeded |