#include<bits/stdc++.h>
using namespace std;
#define pii pair<int ,int>
#define pb push_back
const int maxN = 2600;
int n, m;
int a[maxN][maxN];
int d[maxN][maxN];
int c[maxN][maxN];
string s;
int cnt[maxN];
int getDSum(int x1, int y1, int x2, int y2) {
if (x2 > n || y2 > m) return 0;
return d[x2][y2] + d[x1 - 1][y1 - 1] - d[x1 - 1][y2] - d[x2][y1 - 1];
}
int getCSum(int x1, int y1, int x2, int y2) {
x1 = max(x1, 1);
y1 = max(y1, 1);
return c[x2][y2] + c[x1 - 1][y1 - 1] - c[x1 - 1][y2] - c[x2][y1 - 1];
}
bool check(int x, int y) {
for (int i = 1; i<=n; i++) {
for (int j = 1; j<=m; j++) {
c[i][j] = 0;
}
}
for (int i = 1; i<=n; i++) {
for (int j = 1; j<=m; j++) {
if (getDSum(i, j, i + x - 1, j + y - 1) == x * y) c[i][j] = 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j<=m; j++) {
c[i][j] += c[i - 1][j] + c[i][j - 1] - c[i - 1][j - 1];
}
}
for (int i = 1; i<=n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] && getCSum(i - x + 1, j - y + 1, i, j) == 0) return false;
}
}
return true;
}
int heuristic1() {
vector<pii> gornji;
for (int i = 1; i<=n; i++) {
for (int j = 1; j<=m; j++) {
if (a[i - 1][j] == 0 && a[i][j - 1] == 0 && a[i][j] == 1) gornji.pb({i, j});
}
}
random_shuffle(gornji.begin(), gornji.end());
for (int i = 1; i <=n; i++) {
cnt[i] = m;
}
for (int i = 0; i < min(400, (int) gornji.size()); i++) {
for (int len = 1; len <= n; len++) {
int x = gornji[i].first;
int y = gornji[i].second;
int lo = -1;
int ro = m + 1;
while(lo < ro - 1) {
int mid = lo + ro >> 1;
if (getDSum(x, y, x + len - 1, y + mid - 1) == len * mid) lo = mid; else ro = mid;
}
cnt[len] = min(cnt[len], lo);
}
}
int ans = 0;
for (int i = 1; i<=n; i++) {
ans = max(ans, i * cnt[i]);
}
return ans;
}
int heuristic() {
int minW = n;
int minH = m;
for (int i = 1; i<=n; i++) {
int cur = 0;
for (int j = 1; j<=m; j++) {
if (a[i][j] == 1) cur++; else {
if (cur > 0) {
minH = min(minH, cur);
cur = 0;
}
}
}
if (cur > 0) minH = min(minH, cur);
}
for (int j = 1; j<=m; j++) {
int cur = 0;
for (int i = 1; i<=n; i++) {
if (a[i][j] == 1) cur++; else {
if (cur > 0) {
minW = min(minW, cur);
cur = 0;
}
}
}
if (cur > 0) minW = min(minW, cur);
}
int ans = minW * minH;
if (!check(minW, minH)) return heuristic1();
return ans;
}
int main() {
cin >> n >> m;
for (int i = 1; i <=n; i++) {
cin >> s;
for (int j = 0; j < m; j++) {
a[i][j + 1] = s[j] - '0';
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j<=m; j++) {
d[i][j] = d[i - 1][j] + d[i][j - 1] + a[i][j] - d[i - 1][j - 1];
}
}
if (n > 450 || m > 450) {
cout << heuristic() << endl;
return 0;
}
int ans = 0;
int width = m;
for (int i = 1; i <=n; i++) {
while (width > 0 && !check(i, width)) width--;
ans = max(ans, i * width);
}
cout << ans << endl;
}
Compilation message
bomb.cpp: In function 'int heuristic1()':
bomb.cpp:83:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
83 | int mid = lo + ro >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
16 ms |
30452 KB |
Output is correct |
4 |
Correct |
23 ms |
30420 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
572 KB |
Output is correct |
9 |
Correct |
1 ms |
568 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
440 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
480 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
5 ms |
1108 KB |
Output is correct |
18 |
Correct |
4 ms |
1108 KB |
Output is correct |
19 |
Correct |
10 ms |
1492 KB |
Output is correct |
20 |
Correct |
7 ms |
1468 KB |
Output is correct |
21 |
Correct |
5 ms |
980 KB |
Output is correct |
22 |
Correct |
6 ms |
1216 KB |
Output is correct |
23 |
Correct |
10 ms |
1620 KB |
Output is correct |
24 |
Correct |
6 ms |
1336 KB |
Output is correct |
25 |
Correct |
13 ms |
1748 KB |
Output is correct |
26 |
Correct |
9 ms |
1596 KB |
Output is correct |
27 |
Correct |
173 ms |
4848 KB |
Output is correct |
28 |
Correct |
310 ms |
5200 KB |
Output is correct |
29 |
Correct |
374 ms |
6580 KB |
Output is correct |
30 |
Correct |
817 ms |
7892 KB |
Output is correct |
31 |
Correct |
619 ms |
6224 KB |
Output is correct |
32 |
Correct |
552 ms |
7152 KB |
Output is correct |
33 |
Correct |
993 ms |
8284 KB |
Output is correct |
34 |
Correct |
124 ms |
5604 KB |
Output is correct |
35 |
Correct |
650 ms |
8284 KB |
Output is correct |
36 |
Correct |
802 ms |
8280 KB |
Output is correct |
37 |
Correct |
1 ms |
564 KB |
Output is correct |
38 |
Correct |
319 ms |
78328 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Incorrect |
34 ms |
20464 KB |
Output isn't correct |
41 |
Correct |
1 ms |
568 KB |
Output is correct |
42 |
Correct |
13 ms |
1620 KB |
Output is correct |
43 |
Correct |
403 ms |
78420 KB |
Output is correct |
44 |
Execution timed out |
1079 ms |
8240 KB |
Time limit exceeded |
45 |
Incorrect |
478 ms |
78352 KB |
Output isn't correct |
46 |
Correct |
373 ms |
78136 KB |
Output is correct |
47 |
Incorrect |
415 ms |
78412 KB |
Output isn't correct |
48 |
Correct |
372 ms |
78368 KB |
Output is correct |
49 |
Correct |
356 ms |
78416 KB |
Output is correct |
50 |
Incorrect |
351 ms |
78436 KB |
Output isn't correct |
51 |
Incorrect |
440 ms |
76644 KB |
Output isn't correct |
52 |
Correct |
355 ms |
76628 KB |
Output is correct |
53 |
Incorrect |
336 ms |
76616 KB |
Output isn't correct |
54 |
Correct |
409 ms |
76644 KB |
Output is correct |
55 |
Incorrect |
343 ms |
76532 KB |
Output isn't correct |
56 |
Correct |
309 ms |
76632 KB |
Output is correct |
57 |
Incorrect |
359 ms |
76612 KB |
Output isn't correct |
58 |
Incorrect |
359 ms |
76588 KB |
Output isn't correct |
59 |
Correct |
335 ms |
76648 KB |
Output is correct |
60 |
Correct |
292 ms |
76628 KB |
Output is correct |
61 |
Correct |
349 ms |
76756 KB |
Output is correct |
62 |
Correct |
308 ms |
76632 KB |
Output is correct |
63 |
Correct |
340 ms |
76600 KB |
Output is correct |
64 |
Correct |
291 ms |
76544 KB |
Output is correct |
65 |
Correct |
333 ms |
76636 KB |
Output is correct |
66 |
Incorrect |
314 ms |
76520 KB |
Output isn't correct |
67 |
Correct |
321 ms |
76644 KB |
Output is correct |
68 |
Incorrect |
337 ms |
76640 KB |
Output isn't correct |
69 |
Correct |
332 ms |
76632 KB |
Output is correct |
70 |
Incorrect |
203 ms |
61368 KB |
Output isn't correct |
71 |
Incorrect |
273 ms |
76732 KB |
Output isn't correct |
72 |
Incorrect |
277 ms |
76528 KB |
Output isn't correct |
73 |
Incorrect |
291 ms |
76548 KB |
Output isn't correct |
74 |
Incorrect |
283 ms |
76640 KB |
Output isn't correct |
75 |
Incorrect |
274 ms |
76872 KB |
Output isn't correct |
76 |
Incorrect |
343 ms |
76676 KB |
Output isn't correct |
77 |
Incorrect |
286 ms |
76596 KB |
Output isn't correct |
78 |
Incorrect |
315 ms |
76652 KB |
Output isn't correct |
79 |
Incorrect |
300 ms |
76528 KB |
Output isn't correct |
80 |
Incorrect |
278 ms |
76632 KB |
Output isn't correct |
81 |
Incorrect |
311 ms |
76636 KB |
Output isn't correct |
82 |
Incorrect |
286 ms |
76648 KB |
Output isn't correct |
83 |
Incorrect |
292 ms |
76564 KB |
Output isn't correct |
84 |
Incorrect |
289 ms |
76620 KB |
Output isn't correct |
85 |
Incorrect |
309 ms |
76580 KB |
Output isn't correct |
86 |
Incorrect |
274 ms |
76632 KB |
Output isn't correct |
87 |
Incorrect |
298 ms |
76596 KB |
Output isn't correct |
88 |
Incorrect |
272 ms |
76536 KB |
Output isn't correct |
89 |
Incorrect |
284 ms |
76536 KB |
Output isn't correct |
90 |
Incorrect |
205 ms |
61368 KB |
Output isn't correct |
91 |
Incorrect |
275 ms |
76612 KB |
Output isn't correct |
92 |
Incorrect |
301 ms |
76524 KB |
Output isn't correct |
93 |
Incorrect |
271 ms |
76528 KB |
Output isn't correct |
94 |
Incorrect |
277 ms |
76660 KB |
Output isn't correct |
95 |
Incorrect |
283 ms |
76536 KB |
Output isn't correct |
96 |
Incorrect |
288 ms |
76628 KB |
Output isn't correct |
97 |
Incorrect |
284 ms |
76516 KB |
Output isn't correct |
98 |
Incorrect |
270 ms |
76532 KB |
Output isn't correct |
99 |
Incorrect |
275 ms |
76620 KB |
Output isn't correct |
100 |
Incorrect |
271 ms |
76528 KB |
Output isn't correct |