#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
vector <vector <int>> a;
const int Inf = 1e9;
int mx = -Inf;
int mn = Inf;
int n, m;
void rev1() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m / 2; ++j) {
swap(a[i][j], a[i][m - j - 1]);
}
}
}
void rev2() {
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < m; ++j) {
swap(a[i][j], a[n - i - 1][j]);
}
}
}
int ans = Inf;
void bins() {
int l = -1, r = Inf;
while (r - l > 1) {
int mid = (l + r) >> 1;
int cr = m;
bool fl = true;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (abs(mx - a[i][j]) > mid) cr = min(cr, j);
}
for (int j = cr; j < m; ++j) {
if (abs(mn - a[i][j]) > mid) {
fl = false;
}
}
}
if (fl) r = mid;
else l = mid;
}
ans = min(ans, r);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n >> m;
a.resize(n, vector <int> (m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
}
ans = mx - mn;
bins();
rev1();
bins();
rev2();
bins();
rev1();
bins();
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
16 ms |
632 KB |
Output is correct |
17 |
Correct |
12 ms |
760 KB |
Output is correct |
18 |
Correct |
12 ms |
760 KB |
Output is correct |
19 |
Correct |
12 ms |
836 KB |
Output is correct |
20 |
Correct |
12 ms |
760 KB |
Output is correct |
21 |
Correct |
16 ms |
888 KB |
Output is correct |
22 |
Correct |
15 ms |
924 KB |
Output is correct |
23 |
Correct |
25 ms |
888 KB |
Output is correct |
24 |
Correct |
14 ms |
804 KB |
Output is correct |
25 |
Correct |
18 ms |
888 KB |
Output is correct |
26 |
Correct |
16 ms |
888 KB |
Output is correct |
27 |
Correct |
16 ms |
888 KB |
Output is correct |
28 |
Correct |
16 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
16 ms |
632 KB |
Output is correct |
17 |
Correct |
12 ms |
760 KB |
Output is correct |
18 |
Correct |
12 ms |
760 KB |
Output is correct |
19 |
Correct |
12 ms |
836 KB |
Output is correct |
20 |
Correct |
12 ms |
760 KB |
Output is correct |
21 |
Correct |
16 ms |
888 KB |
Output is correct |
22 |
Correct |
15 ms |
924 KB |
Output is correct |
23 |
Correct |
25 ms |
888 KB |
Output is correct |
24 |
Correct |
14 ms |
804 KB |
Output is correct |
25 |
Correct |
18 ms |
888 KB |
Output is correct |
26 |
Correct |
16 ms |
888 KB |
Output is correct |
27 |
Correct |
16 ms |
888 KB |
Output is correct |
28 |
Correct |
16 ms |
888 KB |
Output is correct |
29 |
Correct |
1077 ms |
37340 KB |
Output is correct |
30 |
Correct |
1062 ms |
36472 KB |
Output is correct |
31 |
Correct |
1189 ms |
39332 KB |
Output is correct |
32 |
Correct |
1048 ms |
39416 KB |
Output is correct |
33 |
Correct |
890 ms |
34184 KB |
Output is correct |
34 |
Correct |
1130 ms |
39504 KB |
Output is correct |
35 |
Correct |
1466 ms |
54936 KB |
Output is correct |
36 |
Correct |
1240 ms |
47864 KB |
Output is correct |
37 |
Correct |
1642 ms |
55124 KB |
Output is correct |