#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m, mn = 1e9, a[N][N], b[N][N], f[N];
bool check0(int x) {
f[0] = 1e9;
int _mn, _mx;
_mn = 1e9, _mx = 0;
for (int i = 1; i <= n; ++i) {
int mx = 0; f[i] = 0;
for (int j = 1; j <= m; ++j) {
mx = max(mx, b[i][j]);
if (mx - mn > x) break;
f[i]++;
}
f[i] = min(f[i], f[i - 1]);
for (int j = f[i] + 1; j <= m; ++j) {
_mn = min(_mn, b[i][j]);
_mx = max(_mx, b[i][j]);
}
}
return _mx - _mn <= x;
}
bool check1(int x) {
bool ok = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
b[i][j] = a[i][j];
}
}
ok |= check0(x);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
b[i][j] = a[i][m - j + 1];
}
}
ok |= check0(x);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
b[i][j] = a[n - i + 1][j];
}
}
ok |= check0(x);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
b[i][j] = a[n - i + 1][m - j + 1];
}
}
ok |= check0(x);
return ok;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j], mn = min(mn, a[i][j]);
}
}
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) >> 1;
if (check1(mid)) r = mid;
else l = mid + 1;
}
cout << l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
7 ms |
2396 KB |
Output is correct |
17 |
Correct |
14 ms |
2444 KB |
Output is correct |
18 |
Correct |
17 ms |
2648 KB |
Output is correct |
19 |
Correct |
10 ms |
2652 KB |
Output is correct |
20 |
Correct |
10 ms |
2392 KB |
Output is correct |
21 |
Correct |
11 ms |
2720 KB |
Output is correct |
22 |
Correct |
10 ms |
2652 KB |
Output is correct |
23 |
Correct |
10 ms |
2648 KB |
Output is correct |
24 |
Correct |
8 ms |
2436 KB |
Output is correct |
25 |
Correct |
10 ms |
2780 KB |
Output is correct |
26 |
Correct |
11 ms |
2648 KB |
Output is correct |
27 |
Correct |
11 ms |
2652 KB |
Output is correct |
28 |
Correct |
11 ms |
2788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
7 ms |
2396 KB |
Output is correct |
17 |
Correct |
14 ms |
2444 KB |
Output is correct |
18 |
Correct |
17 ms |
2648 KB |
Output is correct |
19 |
Correct |
10 ms |
2652 KB |
Output is correct |
20 |
Correct |
10 ms |
2392 KB |
Output is correct |
21 |
Correct |
11 ms |
2720 KB |
Output is correct |
22 |
Correct |
10 ms |
2652 KB |
Output is correct |
23 |
Correct |
10 ms |
2648 KB |
Output is correct |
24 |
Correct |
8 ms |
2436 KB |
Output is correct |
25 |
Correct |
10 ms |
2780 KB |
Output is correct |
26 |
Correct |
11 ms |
2648 KB |
Output is correct |
27 |
Correct |
11 ms |
2652 KB |
Output is correct |
28 |
Correct |
11 ms |
2788 KB |
Output is correct |
29 |
Correct |
750 ms |
41464 KB |
Output is correct |
30 |
Correct |
709 ms |
43096 KB |
Output is correct |
31 |
Correct |
710 ms |
43344 KB |
Output is correct |
32 |
Correct |
754 ms |
43656 KB |
Output is correct |
33 |
Correct |
636 ms |
38244 KB |
Output is correct |
34 |
Correct |
691 ms |
43640 KB |
Output is correct |
35 |
Correct |
937 ms |
51600 KB |
Output is correct |
36 |
Correct |
731 ms |
48664 KB |
Output is correct |
37 |
Correct |
901 ms |
51560 KB |
Output is correct |