#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef home
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<vector<int>> h(n, vector<int>(m));
for (auto& v : h)
for (auto& x : v)
cin >> x;
int mn_h = INT_MAX, mx_h = INT_MIN;
for (const auto& v : h) {
auto p = minmax_element(v.begin(), v.end());
mn_h = min(mn_h, *p.first);
mx_h = max(mx_h, *p.second);
}
auto ok = [&](int lim) {
int64_t pivot = m;
for (const auto& v : h) {
pivot = min(pivot, find_if(v.begin(), v.begin() + pivot, [&](auto& x) { return x > mn_h + lim; }) - v.begin());
if (any_of(v.begin() + pivot, v.end(), [&](auto& x) { return x < mx_h - lim; }))
return false;
}
return true;
};
int ans = mx_h - mn_h;
for (int t = 0; t < 4; ++t) {
if (t & 1)
for (auto& v : h)
reverse(v.begin(), v.end());
else if (t == 2)
reverse(h.begin(), h.end());
int lo = 0, hi = ans;
while (lo != hi) {
int mid = (lo + hi) >> 1;
ok(mid) ? hi = mid : lo = mid + 1;
}
ans = min(ans, lo);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
464 KB |
Output is correct |
17 |
Correct |
5 ms |
756 KB |
Output is correct |
18 |
Correct |
5 ms |
716 KB |
Output is correct |
19 |
Correct |
5 ms |
716 KB |
Output is correct |
20 |
Correct |
5 ms |
588 KB |
Output is correct |
21 |
Correct |
7 ms |
832 KB |
Output is correct |
22 |
Correct |
6 ms |
844 KB |
Output is correct |
23 |
Correct |
6 ms |
848 KB |
Output is correct |
24 |
Correct |
5 ms |
716 KB |
Output is correct |
25 |
Correct |
6 ms |
844 KB |
Output is correct |
26 |
Correct |
8 ms |
848 KB |
Output is correct |
27 |
Correct |
6 ms |
848 KB |
Output is correct |
28 |
Correct |
7 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
464 KB |
Output is correct |
17 |
Correct |
5 ms |
756 KB |
Output is correct |
18 |
Correct |
5 ms |
716 KB |
Output is correct |
19 |
Correct |
5 ms |
716 KB |
Output is correct |
20 |
Correct |
5 ms |
588 KB |
Output is correct |
21 |
Correct |
7 ms |
832 KB |
Output is correct |
22 |
Correct |
6 ms |
844 KB |
Output is correct |
23 |
Correct |
6 ms |
848 KB |
Output is correct |
24 |
Correct |
5 ms |
716 KB |
Output is correct |
25 |
Correct |
6 ms |
844 KB |
Output is correct |
26 |
Correct |
8 ms |
848 KB |
Output is correct |
27 |
Correct |
6 ms |
848 KB |
Output is correct |
28 |
Correct |
7 ms |
844 KB |
Output is correct |
29 |
Correct |
389 ms |
37340 KB |
Output is correct |
30 |
Correct |
374 ms |
36292 KB |
Output is correct |
31 |
Correct |
410 ms |
39092 KB |
Output is correct |
32 |
Correct |
398 ms |
39280 KB |
Output is correct |
33 |
Correct |
344 ms |
34024 KB |
Output is correct |
34 |
Correct |
406 ms |
39364 KB |
Output is correct |
35 |
Correct |
570 ms |
54860 KB |
Output is correct |
36 |
Correct |
473 ms |
47644 KB |
Output is correct |
37 |
Correct |
567 ms |
54844 KB |
Output is correct |