#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
const int inf = 1e9 + 7;
int n, m;
int a[N][N], tmp[N][N];
int mnR[N][N], mxR[N][N];
int pos[N];
void _rotate() {
for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) tmp[j][n + 1 - i] = a[i][j];
swap(n, m);
for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) a[i][j] = tmp[i][j];
}
int ans = inf;
int mn = inf;
bool check(int val) {
for (int i = 1; i <= n; ++i) {
pos[i] = m + 1;
for (int j = 1; j <= m; ++j) if (a[i][j] > mn + val) { pos[i] = j; break; }
}
for (int i = 1; i <= n; ++i) {
mnR[i][m + 1] = inf; mxR[i][m + 1] = 0;
for (int j = m; j >= 1; --j) {
mnR[i][j] = min(mnR[i][j + 1], a[i][j]);
mxR[i][j] = max(mxR[i][j + 1], a[i][j]);
}
}
int cur = m;
int cur_min = inf, cur_max = 0;
for (int i = 1; i <= n; ++i) {
cur = min(cur, pos[i] - 1);
cur_min = min(cur_min, mnR[i][cur + 1]);
cur_max = max(cur_max, mxR[i][cur + 1]);
}
return cur_max - cur_min <= val;
}
void solve() {
int l = 0, r = inf;
while(l < r) {
int mid = ((l + r) >> 1);
if (check(mid)) r = mid; else l = mid + 1;
}
if (check(l)) ans = min(ans, l);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
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]);
}
solve();
_rotate();
solve();
_rotate();
solve();
_rotate();
solve();
printf("%d\n", ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
64996 KB |
Output is correct |
2 |
Correct |
0 ms |
64996 KB |
Output is correct |
3 |
Correct |
0 ms |
64996 KB |
Output is correct |
4 |
Correct |
0 ms |
64996 KB |
Output is correct |
5 |
Correct |
0 ms |
64996 KB |
Output is correct |
6 |
Correct |
0 ms |
64996 KB |
Output is correct |
7 |
Correct |
0 ms |
64996 KB |
Output is correct |
8 |
Correct |
0 ms |
64996 KB |
Output is correct |
9 |
Correct |
0 ms |
64996 KB |
Output is correct |
10 |
Correct |
0 ms |
64996 KB |
Output is correct |
11 |
Correct |
0 ms |
64996 KB |
Output is correct |
12 |
Correct |
0 ms |
64996 KB |
Output is correct |
13 |
Correct |
0 ms |
64996 KB |
Output is correct |
14 |
Correct |
0 ms |
64996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
64996 KB |
Output is correct |
2 |
Correct |
0 ms |
64996 KB |
Output is correct |
3 |
Correct |
0 ms |
64996 KB |
Output is correct |
4 |
Correct |
0 ms |
64996 KB |
Output is correct |
5 |
Correct |
0 ms |
64996 KB |
Output is correct |
6 |
Correct |
0 ms |
64996 KB |
Output is correct |
7 |
Correct |
0 ms |
64996 KB |
Output is correct |
8 |
Correct |
0 ms |
64996 KB |
Output is correct |
9 |
Correct |
0 ms |
64996 KB |
Output is correct |
10 |
Correct |
0 ms |
64996 KB |
Output is correct |
11 |
Correct |
0 ms |
64996 KB |
Output is correct |
12 |
Correct |
0 ms |
64996 KB |
Output is correct |
13 |
Correct |
0 ms |
64996 KB |
Output is correct |
14 |
Correct |
0 ms |
64996 KB |
Output is correct |
15 |
Correct |
0 ms |
64996 KB |
Output is correct |
16 |
Correct |
19 ms |
64996 KB |
Output is correct |
17 |
Correct |
19 ms |
64996 KB |
Output is correct |
18 |
Correct |
23 ms |
64996 KB |
Output is correct |
19 |
Correct |
19 ms |
64996 KB |
Output is correct |
20 |
Correct |
23 ms |
64996 KB |
Output is correct |
21 |
Correct |
26 ms |
64996 KB |
Output is correct |
22 |
Correct |
19 ms |
64996 KB |
Output is correct |
23 |
Correct |
16 ms |
64996 KB |
Output is correct |
24 |
Correct |
23 ms |
64996 KB |
Output is correct |
25 |
Correct |
19 ms |
64996 KB |
Output is correct |
26 |
Correct |
13 ms |
64996 KB |
Output is correct |
27 |
Correct |
26 ms |
64996 KB |
Output is correct |
28 |
Correct |
19 ms |
64996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
64996 KB |
Output is correct |
2 |
Correct |
0 ms |
64996 KB |
Output is correct |
3 |
Correct |
0 ms |
64996 KB |
Output is correct |
4 |
Correct |
0 ms |
64996 KB |
Output is correct |
5 |
Correct |
0 ms |
64996 KB |
Output is correct |
6 |
Correct |
0 ms |
64996 KB |
Output is correct |
7 |
Correct |
0 ms |
64996 KB |
Output is correct |
8 |
Correct |
0 ms |
64996 KB |
Output is correct |
9 |
Correct |
0 ms |
64996 KB |
Output is correct |
10 |
Correct |
0 ms |
64996 KB |
Output is correct |
11 |
Correct |
0 ms |
64996 KB |
Output is correct |
12 |
Correct |
0 ms |
64996 KB |
Output is correct |
13 |
Correct |
0 ms |
64996 KB |
Output is correct |
14 |
Correct |
0 ms |
64996 KB |
Output is correct |
15 |
Correct |
0 ms |
64996 KB |
Output is correct |
16 |
Correct |
19 ms |
64996 KB |
Output is correct |
17 |
Correct |
19 ms |
64996 KB |
Output is correct |
18 |
Correct |
23 ms |
64996 KB |
Output is correct |
19 |
Correct |
19 ms |
64996 KB |
Output is correct |
20 |
Correct |
23 ms |
64996 KB |
Output is correct |
21 |
Correct |
26 ms |
64996 KB |
Output is correct |
22 |
Correct |
19 ms |
64996 KB |
Output is correct |
23 |
Correct |
16 ms |
64996 KB |
Output is correct |
24 |
Correct |
23 ms |
64996 KB |
Output is correct |
25 |
Correct |
19 ms |
64996 KB |
Output is correct |
26 |
Correct |
13 ms |
64996 KB |
Output is correct |
27 |
Correct |
26 ms |
64996 KB |
Output is correct |
28 |
Correct |
19 ms |
64996 KB |
Output is correct |
29 |
Correct |
2423 ms |
64996 KB |
Output is correct |
30 |
Correct |
2419 ms |
64996 KB |
Output is correct |
31 |
Correct |
2676 ms |
64996 KB |
Output is correct |
32 |
Correct |
2499 ms |
64996 KB |
Output is correct |
33 |
Correct |
2296 ms |
64996 KB |
Output is correct |
34 |
Correct |
2506 ms |
64996 KB |
Output is correct |
35 |
Correct |
2533 ms |
64996 KB |
Output is correct |
36 |
Correct |
2189 ms |
64996 KB |
Output is correct |
37 |
Correct |
2639 ms |
64996 KB |
Output is correct |