#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
int n, m, mn, mx, ans;
int a[2010][2010];
bool check(int k) {
int r = m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= r; j++) {
if (a[i][j] > mn + k) {
r = j;
break ;
}
}
for (int j = m; j >= r; j--) {
if (a[i][j] < mx - k) {
return 0;
}
}
}
return 1;
}
void calc() {
int l = 0, r = ans;
while (l <= r) {
int k = (l + r) / 2;
if (check(k)) {ans = k; r = k - 1;}
else l = k + 1;
}
}
void flip1() {
for (int i = 1; i <= n / 2; i++)
for (int j = 1; j <= m; j++)
swap(a[i][j], a[n-i+1][j]);
}
void flip2() {
for (int j = 1; j <= m / 2; j++)
for (int i = 1; i <= n; i++)
swap(a[i][j], a[i][m-j+1]);
}
void print() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << a[i][j] << ' ';
}
cout << '\n';
} cout << '\n';
}
void solve () {
mn = INT_MAX, mx = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
ans = mx - mn;
calc();
flip1();
// print();
calc();
flip2();
// print();
calc();
flip1();
// print();
calc();
cout << ans;
}
signed main() {IOS solve(); return 0;}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |