#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 = 1; i < min(2700, (int) gornji.size()); i++) {
for (int len = 1; len <= n; len++) {
int width = m;
int x = gornji[i].first;
int y = gornji[i].second;
while (width > 0 && getDSum(x, y, x + len - 1, y + width - 1) != len * width) width--;
cnt[len] = min(cnt[len], width);
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
13 ms |
30436 KB |
Output is correct |
4 |
Correct |
13 ms |
30404 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
5 ms |
1492 KB |
Output is correct |
20 |
Correct |
6 ms |
1520 KB |
Output is correct |
21 |
Correct |
3 ms |
980 KB |
Output is correct |
22 |
Correct |
5 ms |
1236 KB |
Output is correct |
23 |
Correct |
9 ms |
1620 KB |
Output is correct |
24 |
Correct |
5 ms |
1364 KB |
Output is correct |
25 |
Correct |
13 ms |
1620 KB |
Output is correct |
26 |
Correct |
11 ms |
1628 KB |
Output is correct |
27 |
Correct |
131 ms |
4692 KB |
Output is correct |
28 |
Correct |
248 ms |
5076 KB |
Output is correct |
29 |
Correct |
357 ms |
6452 KB |
Output is correct |
30 |
Correct |
655 ms |
7704 KB |
Output is correct |
31 |
Correct |
519 ms |
6100 KB |
Output is correct |
32 |
Correct |
481 ms |
7000 KB |
Output is correct |
33 |
Correct |
801 ms |
8100 KB |
Output is correct |
34 |
Correct |
126 ms |
5524 KB |
Output is correct |
35 |
Correct |
549 ms |
8092 KB |
Output is correct |
36 |
Correct |
665 ms |
8088 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
267 ms |
76628 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Incorrect |
34 ms |
19796 KB |
Output isn't correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
11 ms |
1636 KB |
Output is correct |
43 |
Correct |
300 ms |
76588 KB |
Output is correct |
44 |
Correct |
939 ms |
8088 KB |
Output is correct |
45 |
Execution timed out |
1089 ms |
76600 KB |
Time limit exceeded |
46 |
Correct |
294 ms |
76540 KB |
Output is correct |
47 |
Execution timed out |
1090 ms |
76580 KB |
Time limit exceeded |
48 |
Execution timed out |
1096 ms |
76748 KB |
Time limit exceeded |
49 |
Correct |
270 ms |
76684 KB |
Output is correct |
50 |
Execution timed out |
1091 ms |
76676 KB |
Time limit exceeded |
51 |
Execution timed out |
1095 ms |
76620 KB |
Time limit exceeded |
52 |
Execution timed out |
1086 ms |
76628 KB |
Time limit exceeded |
53 |
Execution timed out |
1092 ms |
76640 KB |
Time limit exceeded |
54 |
Execution timed out |
1087 ms |
76752 KB |
Time limit exceeded |
55 |
Execution timed out |
1081 ms |
76696 KB |
Time limit exceeded |
56 |
Correct |
268 ms |
76636 KB |
Output is correct |
57 |
Execution timed out |
1091 ms |
76568 KB |
Time limit exceeded |
58 |
Execution timed out |
1075 ms |
76620 KB |
Time limit exceeded |
59 |
Execution timed out |
1091 ms |
76620 KB |
Time limit exceeded |
60 |
Correct |
269 ms |
76632 KB |
Output is correct |
61 |
Correct |
277 ms |
76636 KB |
Output is correct |
62 |
Correct |
278 ms |
76556 KB |
Output is correct |
63 |
Correct |
269 ms |
76636 KB |
Output is correct |
64 |
Correct |
273 ms |
76632 KB |
Output is correct |
65 |
Execution timed out |
1090 ms |
76536 KB |
Time limit exceeded |
66 |
Incorrect |
403 ms |
76600 KB |
Output isn't correct |
67 |
Execution timed out |
1091 ms |
76620 KB |
Time limit exceeded |
68 |
Execution timed out |
1091 ms |
76572 KB |
Time limit exceeded |
69 |
Execution timed out |
1088 ms |
76636 KB |
Time limit exceeded |
70 |
Incorrect |
192 ms |
61360 KB |
Output isn't correct |
71 |
Incorrect |
278 ms |
76648 KB |
Output isn't correct |
72 |
Incorrect |
263 ms |
76632 KB |
Output isn't correct |
73 |
Incorrect |
265 ms |
76584 KB |
Output isn't correct |
74 |
Incorrect |
269 ms |
76748 KB |
Output isn't correct |
75 |
Incorrect |
262 ms |
76528 KB |
Output isn't correct |
76 |
Incorrect |
269 ms |
76528 KB |
Output isn't correct |
77 |
Incorrect |
272 ms |
76660 KB |
Output isn't correct |
78 |
Incorrect |
265 ms |
76540 KB |
Output isn't correct |
79 |
Incorrect |
272 ms |
76632 KB |
Output isn't correct |
80 |
Incorrect |
268 ms |
76700 KB |
Output isn't correct |
81 |
Incorrect |
271 ms |
76588 KB |
Output isn't correct |
82 |
Incorrect |
270 ms |
76524 KB |
Output isn't correct |
83 |
Incorrect |
273 ms |
76620 KB |
Output isn't correct |
84 |
Incorrect |
258 ms |
76636 KB |
Output isn't correct |
85 |
Incorrect |
273 ms |
76560 KB |
Output isn't correct |
86 |
Incorrect |
285 ms |
76632 KB |
Output isn't correct |
87 |
Incorrect |
267 ms |
76512 KB |
Output isn't correct |
88 |
Incorrect |
265 ms |
76584 KB |
Output isn't correct |
89 |
Incorrect |
266 ms |
76532 KB |
Output isn't correct |
90 |
Incorrect |
179 ms |
61444 KB |
Output isn't correct |
91 |
Incorrect |
273 ms |
76532 KB |
Output isn't correct |
92 |
Incorrect |
261 ms |
76636 KB |
Output isn't correct |
93 |
Incorrect |
262 ms |
76752 KB |
Output isn't correct |
94 |
Incorrect |
264 ms |
76728 KB |
Output isn't correct |
95 |
Incorrect |
281 ms |
76624 KB |
Output isn't correct |
96 |
Incorrect |
291 ms |
76632 KB |
Output isn't correct |
97 |
Incorrect |
264 ms |
76532 KB |
Output isn't correct |
98 |
Incorrect |
264 ms |
76572 KB |
Output isn't correct |
99 |
Incorrect |
264 ms |
76600 KB |
Output isn't correct |
100 |
Incorrect |
265 ms |
76556 KB |
Output isn't correct |