#include <bits/stdc++.h>
#define pb push_back
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define x first
#define y second
#define int long long
#define nl "\n"
using namespace std;
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair <ll, ll> pii;
const int N = (int)1e6 + 7;
const int M = (int)5e6 + 7;
const ll MOD = (ll)1e9 + 7;
const int inf = (int)1e9 + 7;
const ll INF = (ll)3e18 + 7;
pii dir[] = {{-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
int n, m;
char c[2501][2501];
int used[2501][2501], w[2501][2501];
int get(int x1, int y1, int x2, int y2) {
return w[x2][y2] - w[x2][y1-1] - w[x1-1][y2] + w[x1-1][y1-1];
}
void upd(int x1, int y1, int x2, int y2) {
used[x1][y1]++;
used[x1][y2+1]--;
used[x2+1][y1]--;
used[x2+1][y2+1]++;
}
void solve() {
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
cin >> c[i][j];
w[i][j] = w[i-1][j] + w[i][j-1] - w[i-1][j-1] + (c[i][j] == '1');
}
}
int ans = inf;
if(n == 1) {
int cur = 0;
for(int i = 1; i <= m; ++i) {
if(c[1][i] == '1') cur++;
else {
if(cur > 0) ans = min(ans, cur);
cur = 0;
}
}
if(c[1][m] == '1') ans = min(ans, cur);
cout << ans << nl;
return;
}
if(m == 1) {
int cur = 0;
for(int i = 1; i <= n; ++i) {
if(c[i][1] == '1') cur++;
else {
if(cur > 0) ans = min(ans, cur);
cur = 0;
}
}
if(c[n][1] == '1') ans = min(ans, cur);
cout << ans << nl;
return;
}
ans = 0;
int l = 1, r = n;
while(l <= r) {
int e = (l + r) >> 1;
int L = 1, R = m;
bool cur = 0;
while(L <= R) {
int f = (L + R) >> 1;
for(int i = 1; i <= n; ++i) fill(used[i] + 1, used[i] + 1 + m, 0);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(i + e - 1 > n || j + f - 1 > m) break;
int x = get(i, j, i + e - 1, j + f - 1);
if(x == e * f) {
upd(i, j, i + e - 1, j + f - 1);
}
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
used[i][j] += used[i-1][j] + used[i][j-1] - used[i-1][j-1];
}
}
bool ok = 1;
for(int i = 1; i <= n && ok; ++i) {
for(int j = 1; j <= m && ok; ++j) {
if(c[i][j] == '1') ok &= (used[i][j] > 0);
}
}
if(ok) {
cur = 1;
ans = max(ans, e * f);
L = f + 1;
} else
R = f - 1;
}
if(cur)
l = e + 1;
else
r = e - 1;
}
if(n <= 100 && m <= 100) {
for(int e = 1; e <= n; ++e) {
for(int f = 1; f <= n; ++f) {
bool ok = 1;
for(int i = 1; i <= n; ++i) fill(used[i] + 1, used[i] + 1 + m, 0);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(i + e - 1 > n || j + f - 1 > m) break;
int x = get(i, j, i + e - 1, j + f - 1);
if(x == e * f) {
upd(i, j, i + e - 1, j + f - 1);
}
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
used[i][j] += used[i-1][j] + used[i][j-1] - used[i-1][j-1];
}
}
for(int i = 1; i <= n && ok; ++i) {
for(int j = 1; j <= m && ok; ++j) {
if(c[i][j] == '1') ok &= (used[i][j] > 0);
}
}
if(ok) {
ans = max(ans, e * f);
//cout << e << ' ' << f << nl;
}
}
}
}
cout << ans << nl;
}
signed main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
int test = 1;
//cin >> test;
for(int i = 1; i <= test; ++i) {
//cout << "Case " << i << ": ";
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
9 ms |
16464 KB |
Output is correct |
4 |
Correct |
9 ms |
16524 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
464 KB |
Output is correct |
17 |
Correct |
66 ms |
1088 KB |
Output is correct |
18 |
Correct |
72 ms |
1228 KB |
Output is correct |
19 |
Correct |
210 ms |
1360 KB |
Output is correct |
20 |
Correct |
221 ms |
1412 KB |
Output is correct |
21 |
Correct |
50 ms |
976 KB |
Output is correct |
22 |
Correct |
148 ms |
1236 KB |
Output is correct |
23 |
Correct |
351 ms |
1544 KB |
Output is correct |
24 |
Correct |
135 ms |
1268 KB |
Output is correct |
25 |
Correct |
369 ms |
1608 KB |
Output is correct |
26 |
Correct |
389 ms |
1608 KB |
Output is correct |
27 |
Correct |
32 ms |
4688 KB |
Output is correct |
28 |
Correct |
39 ms |
5200 KB |
Output is correct |
29 |
Incorrect |
54 ms |
6496 KB |
Output isn't correct |
30 |
Incorrect |
70 ms |
7972 KB |
Output isn't correct |
31 |
Incorrect |
56 ms |
6328 KB |
Output isn't correct |
32 |
Incorrect |
68 ms |
7120 KB |
Output isn't correct |
33 |
Incorrect |
68 ms |
8420 KB |
Output isn't correct |
34 |
Incorrect |
24 ms |
5312 KB |
Output isn't correct |
35 |
Incorrect |
74 ms |
8400 KB |
Output isn't correct |
36 |
Correct |
105 ms |
8416 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Execution timed out |
1020 ms |
110428 KB |
Time limit exceeded |
39 |
Correct |
1 ms |
464 KB |
Output is correct |
40 |
Incorrect |
442 ms |
22456 KB |
Output isn't correct |
41 |
Correct |
2 ms |
464 KB |
Output is correct |
42 |
Correct |
380 ms |
1544 KB |
Output is correct |
43 |
Execution timed out |
1054 ms |
110408 KB |
Time limit exceeded |
44 |
Incorrect |
76 ms |
8392 KB |
Output isn't correct |
45 |
Execution timed out |
1084 ms |
110344 KB |
Time limit exceeded |
46 |
Execution timed out |
1045 ms |
110408 KB |
Time limit exceeded |
47 |
Execution timed out |
1056 ms |
110376 KB |
Time limit exceeded |
48 |
Execution timed out |
1108 ms |
110404 KB |
Time limit exceeded |
49 |
Execution timed out |
1012 ms |
110372 KB |
Time limit exceeded |
50 |
Execution timed out |
1097 ms |
110408 KB |
Time limit exceeded |
51 |
Execution timed out |
1093 ms |
110336 KB |
Time limit exceeded |
52 |
Execution timed out |
1022 ms |
110408 KB |
Time limit exceeded |
53 |
Execution timed out |
1045 ms |
110408 KB |
Time limit exceeded |
54 |
Execution timed out |
1039 ms |
110408 KB |
Time limit exceeded |
55 |
Execution timed out |
1080 ms |
110312 KB |
Time limit exceeded |
56 |
Execution timed out |
1075 ms |
110424 KB |
Time limit exceeded |
57 |
Execution timed out |
1014 ms |
110408 KB |
Time limit exceeded |
58 |
Execution timed out |
1079 ms |
110504 KB |
Time limit exceeded |
59 |
Execution timed out |
1065 ms |
110408 KB |
Time limit exceeded |
60 |
Execution timed out |
1095 ms |
110304 KB |
Time limit exceeded |
61 |
Execution timed out |
1087 ms |
110420 KB |
Time limit exceeded |
62 |
Execution timed out |
1089 ms |
110356 KB |
Time limit exceeded |
63 |
Execution timed out |
1051 ms |
110352 KB |
Time limit exceeded |
64 |
Execution timed out |
1042 ms |
110432 KB |
Time limit exceeded |
65 |
Execution timed out |
1087 ms |
110312 KB |
Time limit exceeded |
66 |
Execution timed out |
1102 ms |
110408 KB |
Time limit exceeded |
67 |
Execution timed out |
1079 ms |
110436 KB |
Time limit exceeded |
68 |
Execution timed out |
1101 ms |
110472 KB |
Time limit exceeded |
69 |
Execution timed out |
1056 ms |
110328 KB |
Time limit exceeded |
70 |
Execution timed out |
1106 ms |
87376 KB |
Time limit exceeded |
71 |
Execution timed out |
1025 ms |
110416 KB |
Time limit exceeded |
72 |
Execution timed out |
1053 ms |
110408 KB |
Time limit exceeded |
73 |
Execution timed out |
1098 ms |
110316 KB |
Time limit exceeded |
74 |
Execution timed out |
1077 ms |
110424 KB |
Time limit exceeded |
75 |
Execution timed out |
1016 ms |
110408 KB |
Time limit exceeded |
76 |
Execution timed out |
1032 ms |
110356 KB |
Time limit exceeded |
77 |
Execution timed out |
1067 ms |
110340 KB |
Time limit exceeded |
78 |
Execution timed out |
1027 ms |
110448 KB |
Time limit exceeded |
79 |
Execution timed out |
1097 ms |
110352 KB |
Time limit exceeded |
80 |
Execution timed out |
1039 ms |
110408 KB |
Time limit exceeded |
81 |
Execution timed out |
1069 ms |
110412 KB |
Time limit exceeded |
82 |
Execution timed out |
1043 ms |
110332 KB |
Time limit exceeded |
83 |
Execution timed out |
1025 ms |
110408 KB |
Time limit exceeded |
84 |
Execution timed out |
1033 ms |
110408 KB |
Time limit exceeded |
85 |
Execution timed out |
1071 ms |
110420 KB |
Time limit exceeded |
86 |
Execution timed out |
1068 ms |
110396 KB |
Time limit exceeded |
87 |
Execution timed out |
1028 ms |
110396 KB |
Time limit exceeded |
88 |
Execution timed out |
1041 ms |
110536 KB |
Time limit exceeded |
89 |
Execution timed out |
1052 ms |
110368 KB |
Time limit exceeded |
90 |
Execution timed out |
1031 ms |
87432 KB |
Time limit exceeded |
91 |
Execution timed out |
1084 ms |
110404 KB |
Time limit exceeded |
92 |
Execution timed out |
1008 ms |
110392 KB |
Time limit exceeded |
93 |
Execution timed out |
1094 ms |
110408 KB |
Time limit exceeded |
94 |
Execution timed out |
1048 ms |
110472 KB |
Time limit exceeded |
95 |
Execution timed out |
1049 ms |
110424 KB |
Time limit exceeded |
96 |
Execution timed out |
1020 ms |
110404 KB |
Time limit exceeded |
97 |
Execution timed out |
1016 ms |
110440 KB |
Time limit exceeded |
98 |
Execution timed out |
1056 ms |
110408 KB |
Time limit exceeded |
99 |
Execution timed out |
1029 ms |
110416 KB |
Time limit exceeded |
100 |
Execution timed out |
1018 ms |
110408 KB |
Time limit exceeded |