#include <bits/stdc++.h>
using namespace std;
const int NAX_N = 2020;
int n, m;
int a[NAX_N][NAX_N];
int mn_r, mn_c;
bool check0(int d) {
int prev = m;
int mn = 1e9, mx = -1e9;
for (int r = 0; r < n; r++) {
int c = 0;
while (c < prev && a[r][c] <= a[mn_r][mn_c] + d)
c++;
if (r <= mn_r && c <= mn_c)
return false;
prev = c;
while (c < m) {
mn = min(mn, a[r][c]);
mx = max(mx, a[r][c]);
c++;
}
}
return mn + d >= mx;
}
bool check1(int d) {
int prev = -1;
int mn = 1e9, mx = -1e9;
for (int r = n - 1; r >= 0; r--) {
int c = m - 1;
while (c > prev && a[r][c] <= a[mn_r][mn_c] + d)
c--;
if (r >= mn_r && c >= mn_c)
return false;
prev = c;
while (c >= 0) {
mn = min(mn, a[r][c]);
mx = max(mx, a[r][c]);
c--;
}
}
return mn + d >= mx;
}
bool check2(int d) {
int prev = m;
int mn = 1e9, mx = -1e9;
for (int r = n - 1; r >= 0; r--) {
int c = 0;
while (c < prev && a[r][c] <= a[mn_r][mn_c] + d)
c++;
if (r >= mn_r && c <= mn_c)
return false;
prev = c;
while (c < m) {
mn = min(mn, a[r][c]);
mx = max(mx, a[r][c]);
c++;
}
}
return mn + d >= mx;
}
bool check3(int d) {
int prev = -1;
int mn = 1e9, mx = -1e9;
for (int r = 0; r < n; r++) {
int c = m - 1;
while (c > prev && a[r][c] <= a[mn_r][mn_c] + d)
c--;
if (r <= mn_r && c >= mn_c)
return false;
prev = c;
while (c >= 0) {
mn = min(mn, a[r][c]);
mx = max(mx, a[r][c]);
c--;
}
}
return mn + d >= mx;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] < a[mn_r][mn_c]) {
mn_r = i;
mn_c = j;
}
}
int l = -1, r = 1e9;
while (r - l > 1) {
int m = (l + r) / 2;
if (check0(m) || check1(m)
|| check2(m) || check3(m))
r = m;
else
l = m;
}
cout << r;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1372 KB |
Output is correct |
17 |
Correct |
3 ms |
1628 KB |
Output is correct |
18 |
Correct |
3 ms |
1628 KB |
Output is correct |
19 |
Correct |
3 ms |
1628 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
5 ms |
1628 KB |
Output is correct |
22 |
Correct |
4 ms |
1628 KB |
Output is correct |
23 |
Correct |
4 ms |
1628 KB |
Output is correct |
24 |
Correct |
3 ms |
1608 KB |
Output is correct |
25 |
Correct |
4 ms |
1640 KB |
Output is correct |
26 |
Correct |
4 ms |
1628 KB |
Output is correct |
27 |
Correct |
4 ms |
1800 KB |
Output is correct |
28 |
Correct |
4 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
1372 KB |
Output is correct |
17 |
Correct |
3 ms |
1628 KB |
Output is correct |
18 |
Correct |
3 ms |
1628 KB |
Output is correct |
19 |
Correct |
3 ms |
1628 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
5 ms |
1628 KB |
Output is correct |
22 |
Correct |
4 ms |
1628 KB |
Output is correct |
23 |
Correct |
4 ms |
1628 KB |
Output is correct |
24 |
Correct |
3 ms |
1608 KB |
Output is correct |
25 |
Correct |
4 ms |
1640 KB |
Output is correct |
26 |
Correct |
4 ms |
1628 KB |
Output is correct |
27 |
Correct |
4 ms |
1800 KB |
Output is correct |
28 |
Correct |
4 ms |
1628 KB |
Output is correct |
29 |
Correct |
226 ms |
37344 KB |
Output is correct |
30 |
Correct |
226 ms |
37556 KB |
Output is correct |
31 |
Correct |
236 ms |
39248 KB |
Output is correct |
32 |
Correct |
235 ms |
39252 KB |
Output is correct |
33 |
Correct |
198 ms |
34124 KB |
Output is correct |
34 |
Correct |
242 ms |
39252 KB |
Output is correct |
35 |
Correct |
315 ms |
54772 KB |
Output is correct |
36 |
Correct |
287 ms |
50000 KB |
Output is correct |
37 |
Correct |
327 ms |
54772 KB |
Output is correct |