#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m, mx = 0, mn = INF;
vector< vector<int> > a;
void flip_h() {
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n / 2; i++) {
swap(a[n - i + 1][j], a[i][j]);
}
}
}
void flip_v() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m / 2; j++) {
swap(a[i][m - j + 1], a[i][j]);
}
}
}
int check(int k) {
int t = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] < mx - k) {
t = max(t, j + 1);
}
}
for (int j = 1; j < t; j++) {
if (a[i][j] > mn + k) {
return 0;
}
}
}
return 1;
}
int solve() {
int lo = 0, hi = mx - mn;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (check(mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
return lo;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
a.assign(n + 1, vector<int>(m + 2, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
}
int ans = solve();
flip_h();
ans = min(ans, solve());
flip_v();
ans = min(ans, solve());
flip_h();
ans = min(ans, solve());
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
8 ms |
768 KB |
Output is correct |
18 |
Correct |
8 ms |
768 KB |
Output is correct |
19 |
Correct |
9 ms |
840 KB |
Output is correct |
20 |
Correct |
9 ms |
768 KB |
Output is correct |
21 |
Correct |
10 ms |
896 KB |
Output is correct |
22 |
Correct |
11 ms |
896 KB |
Output is correct |
23 |
Correct |
12 ms |
1024 KB |
Output is correct |
24 |
Correct |
10 ms |
896 KB |
Output is correct |
25 |
Correct |
14 ms |
896 KB |
Output is correct |
26 |
Correct |
10 ms |
896 KB |
Output is correct |
27 |
Correct |
9 ms |
896 KB |
Output is correct |
28 |
Correct |
11 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
8 ms |
768 KB |
Output is correct |
18 |
Correct |
8 ms |
768 KB |
Output is correct |
19 |
Correct |
9 ms |
840 KB |
Output is correct |
20 |
Correct |
9 ms |
768 KB |
Output is correct |
21 |
Correct |
10 ms |
896 KB |
Output is correct |
22 |
Correct |
11 ms |
896 KB |
Output is correct |
23 |
Correct |
12 ms |
1024 KB |
Output is correct |
24 |
Correct |
10 ms |
896 KB |
Output is correct |
25 |
Correct |
14 ms |
896 KB |
Output is correct |
26 |
Correct |
10 ms |
896 KB |
Output is correct |
27 |
Correct |
9 ms |
896 KB |
Output is correct |
28 |
Correct |
11 ms |
896 KB |
Output is correct |
29 |
Correct |
541 ms |
37488 KB |
Output is correct |
30 |
Correct |
443 ms |
36616 KB |
Output is correct |
31 |
Correct |
509 ms |
39288 KB |
Output is correct |
32 |
Correct |
454 ms |
39288 KB |
Output is correct |
33 |
Correct |
375 ms |
34168 KB |
Output is correct |
34 |
Correct |
497 ms |
39544 KB |
Output is correct |
35 |
Correct |
758 ms |
54876 KB |
Output is correct |
36 |
Correct |
638 ms |
47876 KB |
Output is correct |
37 |
Correct |
826 ms |
55168 KB |
Output is correct |