#include <bits/stdc++.h>
using namespace std;
const int N = 2003, MAX = 1e9;
int m, n, a[N][N], mi = MAX, mx = 0, ans = MAX;
bool ok(int f) {
int lst = n;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < lst; j++) {
if (mi + f < a[i][j + 1]) {
lst = j;
break;
}
}
for (int j = n; j > lst; j--)
if (mx - f > a[i][j])
return false;
}
return true;
}
void deal() {
int l = 0, r = ans;
while (l <= r) {
int mid = (l + r) / 2;
if (ok(mid)) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
}
void flip_col() {
for (int i = m / 2; i > 0; i--)
for (int j = 1; j <= n; j++)
swap(a[i][j], a[m - i + 1][j]);
}
void flip_row() {
for (int i = 1; i <= m; i++)
for (int j = n / 2; j > 0; j--)
swap(a[i][j], a[i][n - j + 1]);
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> m >> n;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
mi = min(mi, a[i][j]);
mx = max(mx, a[i][j]);
}
}
ans = mx - mi;
deal();
flip_col();
deal();
flip_row();
deal();
flip_col();
deal();
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
2648 KB |
Output is correct |
17 |
Correct |
3 ms |
2648 KB |
Output is correct |
18 |
Correct |
3 ms |
2652 KB |
Output is correct |
19 |
Correct |
4 ms |
2648 KB |
Output is correct |
20 |
Correct |
3 ms |
2648 KB |
Output is correct |
21 |
Correct |
4 ms |
2904 KB |
Output is correct |
22 |
Correct |
4 ms |
2908 KB |
Output is correct |
23 |
Correct |
5 ms |
2904 KB |
Output is correct |
24 |
Correct |
4 ms |
2904 KB |
Output is correct |
25 |
Correct |
5 ms |
2904 KB |
Output is correct |
26 |
Correct |
4 ms |
2908 KB |
Output is correct |
27 |
Correct |
5 ms |
2904 KB |
Output is correct |
28 |
Correct |
5 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
2648 KB |
Output is correct |
17 |
Correct |
3 ms |
2648 KB |
Output is correct |
18 |
Correct |
3 ms |
2652 KB |
Output is correct |
19 |
Correct |
4 ms |
2648 KB |
Output is correct |
20 |
Correct |
3 ms |
2648 KB |
Output is correct |
21 |
Correct |
4 ms |
2904 KB |
Output is correct |
22 |
Correct |
4 ms |
2908 KB |
Output is correct |
23 |
Correct |
5 ms |
2904 KB |
Output is correct |
24 |
Correct |
4 ms |
2904 KB |
Output is correct |
25 |
Correct |
5 ms |
2904 KB |
Output is correct |
26 |
Correct |
4 ms |
2908 KB |
Output is correct |
27 |
Correct |
5 ms |
2904 KB |
Output is correct |
28 |
Correct |
5 ms |
2908 KB |
Output is correct |
29 |
Correct |
233 ms |
37200 KB |
Output is correct |
30 |
Correct |
241 ms |
37536 KB |
Output is correct |
31 |
Correct |
247 ms |
39200 KB |
Output is correct |
32 |
Correct |
253 ms |
39316 KB |
Output is correct |
33 |
Correct |
212 ms |
34896 KB |
Output is correct |
34 |
Correct |
249 ms |
39284 KB |
Output is correct |
35 |
Correct |
350 ms |
54896 KB |
Output is correct |
36 |
Correct |
470 ms |
49392 KB |
Output is correct |
37 |
Correct |
367 ms |
55140 KB |
Output is correct |