#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
int mn = INF;
for (auto& r : a)
for (auto& el : r) {
cin >> el;
mn = min(mn, el);
}
auto check = [&](int dif) {
int othermx = -INF, othermn = INF;
int curmx = m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < curmx; ++j) {
if (a[i][j] - mn > dif) {
curmx = j;
break;
}
}
for (int j = curmx; j < m; ++j) {
othermx = max(othermx, a[i][j]);
othermn = min(othermn, a[i][j]);
}
}
if (othermx - othermn <= dif || curmx == m)
return true;
othermx = -INF, othermn = INF;
curmx = m;
for (int i = n - 1; i >= 0; --i) {
for (int j = 0; j < curmx; ++j) {
if (a[i][j] - mn > dif) {
curmx = j;
break;
}
}
for (int j = curmx; j < m; ++j) {
othermx = max(othermx, a[i][j]);
othermn = min(othermn, a[i][j]);
}
}
if (othermx - othermn <= dif || curmx == m)
return true;
othermx = -INF, othermn = INF;
curmx = -1;
for (int i = 0; i < n; ++i) {
for (int j = m - 1; j > curmx; --j) {
if (a[i][j] - mn > dif) {
curmx = j;
break;
}
}
for (int j = curmx; j >= 0; --j) {
othermx = max(othermx, a[i][j]);
othermn = min(othermn, a[i][j]);
}
}
if (othermx - othermn <= dif || curmx == -1)
return true;
othermx = -INF, othermn = INF;
curmx = -1;
for (int i = n - 1; i >= 0; --i) {
for (int j = m - 1; j > curmx; --j) {
if (a[i][j] - mn > dif) {
curmx = j;
break;
}
}
for (int j = curmx; j >= 0; --j) {
othermx = max(othermx, a[i][j]);
othermn = min(othermn, a[i][j]);
}
}
if (othermx - othermn <= dif || curmx == -1)
return true;
return false;
};
int l = -1, r = INF;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
cout << r;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
7 ms |
860 KB |
Output is correct |
18 |
Correct |
6 ms |
860 KB |
Output is correct |
19 |
Correct |
7 ms |
860 KB |
Output is correct |
20 |
Correct |
6 ms |
860 KB |
Output is correct |
21 |
Correct |
7 ms |
1116 KB |
Output is correct |
22 |
Correct |
9 ms |
1164 KB |
Output is correct |
23 |
Correct |
9 ms |
1164 KB |
Output is correct |
24 |
Correct |
6 ms |
860 KB |
Output is correct |
25 |
Correct |
9 ms |
1116 KB |
Output is correct |
26 |
Correct |
8 ms |
1160 KB |
Output is correct |
27 |
Correct |
6 ms |
1116 KB |
Output is correct |
28 |
Correct |
7 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
7 ms |
860 KB |
Output is correct |
18 |
Correct |
6 ms |
860 KB |
Output is correct |
19 |
Correct |
7 ms |
860 KB |
Output is correct |
20 |
Correct |
6 ms |
860 KB |
Output is correct |
21 |
Correct |
7 ms |
1116 KB |
Output is correct |
22 |
Correct |
9 ms |
1164 KB |
Output is correct |
23 |
Correct |
9 ms |
1164 KB |
Output is correct |
24 |
Correct |
6 ms |
860 KB |
Output is correct |
25 |
Correct |
9 ms |
1116 KB |
Output is correct |
26 |
Correct |
8 ms |
1160 KB |
Output is correct |
27 |
Correct |
6 ms |
1116 KB |
Output is correct |
28 |
Correct |
7 ms |
1116 KB |
Output is correct |
29 |
Correct |
511 ms |
52360 KB |
Output is correct |
30 |
Correct |
610 ms |
50908 KB |
Output is correct |
31 |
Correct |
577 ms |
55576 KB |
Output is correct |
32 |
Correct |
647 ms |
55024 KB |
Output is correct |
33 |
Correct |
456 ms |
47704 KB |
Output is correct |
34 |
Correct |
643 ms |
54996 KB |
Output is correct |
35 |
Correct |
796 ms |
70412 KB |
Output is correct |
36 |
Correct |
768 ms |
61528 KB |
Output is correct |
37 |
Correct |
932 ms |
70548 KB |
Output is correct |