#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll, ll>;
inline void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const ll INF = 1e18;
ll h, w, mn = INF, mx, ans = INF;
vector<vector<ll>> v;
bool chk(ll val) {
for (ll i = 0, tmp = 0; i < h; i++) {
for (ll j = 0; j < w; j++) if (v[i][j] + val < mx) {
tmp = max(tmp, j + 1);
}
for (ll j = 0; j < w; j++) if (v[i][j] > mn + val and j < tmp) {
return false;
}
}
return true;
}
ll cal() {
ll s = 0, e = mx - mn;
while (s < e) {
ll mid = (s + e) / 2;
if (chk(mid)) {
e = mid;
} else {
s = mid + 1;
}
}
return s;
}
int main() {
fastio();
cin >> h >> w;
v.resize(h, vector<ll>(w));
for (ll i = 0; i < h; i++) for (ll j = 0; j < w; j++) {
cin >> v[i][j];
mx = max(v[i][j], mx);
mn = min(v[i][j], mn);
}
ans = min(ans, cal());
reverse(v.begin(), v.end());
ans = min(ans, cal());
for (auto& x : v) {
reverse(x.begin(), x.end());
}
ans = min(ans, cal());
reverse(v.begin(), v.end());
ans = min(ans, cal());
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
448 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 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 |
344 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
448 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 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 |
344 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
1044 KB |
Output is correct |
20 |
Correct |
4 ms |
980 KB |
Output is correct |
21 |
Correct |
6 ms |
1112 KB |
Output is correct |
22 |
Correct |
6 ms |
1116 KB |
Output is correct |
23 |
Correct |
7 ms |
1144 KB |
Output is correct |
24 |
Correct |
6 ms |
860 KB |
Output is correct |
25 |
Correct |
10 ms |
1160 KB |
Output is correct |
26 |
Correct |
7 ms |
1116 KB |
Output is correct |
27 |
Correct |
7 ms |
1140 KB |
Output is correct |
28 |
Correct |
6 ms |
1140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
448 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 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 |
344 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
860 KB |
Output is correct |
19 |
Correct |
5 ms |
1044 KB |
Output is correct |
20 |
Correct |
4 ms |
980 KB |
Output is correct |
21 |
Correct |
6 ms |
1112 KB |
Output is correct |
22 |
Correct |
6 ms |
1116 KB |
Output is correct |
23 |
Correct |
7 ms |
1144 KB |
Output is correct |
24 |
Correct |
6 ms |
860 KB |
Output is correct |
25 |
Correct |
10 ms |
1160 KB |
Output is correct |
26 |
Correct |
7 ms |
1116 KB |
Output is correct |
27 |
Correct |
7 ms |
1140 KB |
Output is correct |
28 |
Correct |
6 ms |
1140 KB |
Output is correct |
29 |
Correct |
280 ms |
48092 KB |
Output is correct |
30 |
Correct |
350 ms |
46928 KB |
Output is correct |
31 |
Correct |
431 ms |
50004 KB |
Output is correct |
32 |
Correct |
279 ms |
49872 KB |
Output is correct |
33 |
Correct |
230 ms |
43348 KB |
Output is correct |
34 |
Correct |
343 ms |
50056 KB |
Output is correct |
35 |
Correct |
573 ms |
61272 KB |
Output is correct |
36 |
Correct |
514 ms |
53292 KB |
Output is correct |
37 |
Correct |
718 ms |
61308 KB |
Output is correct |