#include "bits/stdc++.h"
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)(x).size()
using ll = long long;
const int mod = 1e9+7;
int n, m, mx = 0, mn = (1<<30);
vector<vector<int>> v;
bool chk(int x) {
int last = -1;
for (int i = 0; i < n; ++i) {
int cur = -1;
bool f = false;
for (int j = 0; j < m; ++j) {
if (v[i][j] < mx - x) {
if (v[i][j] > mn + x or f) return false;
cur = j;
} else if (v[i][j] > mn + x) {
if (j <= last) return false;
f = true;
}
}
last = max(last, cur);
}
return true;
}
int go() {
int lo = 0, hi = mx - mn;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (chk(mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
void solve(int tc) {
cin >> n >> m;
v.resize(n, vector<int>(m));
for (auto &x : v) for (auto &y : x) {
cin >> y;
mx = max(mx, y);
mn = min(mn, y);
}
auto a = v;
int ans = mx - mn;
ans = min(ans, go());
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
v[i][j] = a[i][m-j-1];
ans = min(ans, go());
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
v[i][j] = a[n-i-1][j];
ans = min(ans, go());
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
v[i][j] = a[n-i-1][m-j-1];
ans = min(ans, go());
cout << ans << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; ++i) solve(i);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 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 |
588 KB |
Output is correct |
17 |
Correct |
6 ms |
848 KB |
Output is correct |
18 |
Correct |
6 ms |
876 KB |
Output is correct |
19 |
Correct |
8 ms |
844 KB |
Output is correct |
20 |
Correct |
7 ms |
900 KB |
Output is correct |
21 |
Correct |
8 ms |
980 KB |
Output is correct |
22 |
Correct |
9 ms |
972 KB |
Output is correct |
23 |
Correct |
16 ms |
972 KB |
Output is correct |
24 |
Correct |
8 ms |
936 KB |
Output is correct |
25 |
Correct |
13 ms |
972 KB |
Output is correct |
26 |
Correct |
9 ms |
980 KB |
Output is correct |
27 |
Correct |
8 ms |
972 KB |
Output is correct |
28 |
Correct |
8 ms |
972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 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 |
588 KB |
Output is correct |
17 |
Correct |
6 ms |
848 KB |
Output is correct |
18 |
Correct |
6 ms |
876 KB |
Output is correct |
19 |
Correct |
8 ms |
844 KB |
Output is correct |
20 |
Correct |
7 ms |
900 KB |
Output is correct |
21 |
Correct |
8 ms |
980 KB |
Output is correct |
22 |
Correct |
9 ms |
972 KB |
Output is correct |
23 |
Correct |
16 ms |
972 KB |
Output is correct |
24 |
Correct |
8 ms |
936 KB |
Output is correct |
25 |
Correct |
13 ms |
972 KB |
Output is correct |
26 |
Correct |
9 ms |
980 KB |
Output is correct |
27 |
Correct |
8 ms |
972 KB |
Output is correct |
28 |
Correct |
8 ms |
972 KB |
Output is correct |
29 |
Correct |
431 ms |
52252 KB |
Output is correct |
30 |
Correct |
476 ms |
50976 KB |
Output is correct |
31 |
Correct |
636 ms |
54800 KB |
Output is correct |
32 |
Correct |
437 ms |
54820 KB |
Output is correct |
33 |
Correct |
427 ms |
47708 KB |
Output is correct |
34 |
Correct |
579 ms |
54956 KB |
Output is correct |
35 |
Correct |
766 ms |
70492 KB |
Output is correct |
36 |
Correct |
677 ms |
61460 KB |
Output is correct |
37 |
Correct |
1004 ms |
70620 KB |
Output is correct |