// Programmer: Shadow1
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << ' ';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define read_vector(v) for(auto &x : v){cin >> x;}
#define vt vector
#define prq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
//
const int INF = 1e9;
int a[2005][2005];
int mx = 0, mn = INF;
int n, m;
void rotx() {
for(int i=0; i<n/2; ++i) {
for(int j=0; j<m; ++j)
swap(a[i][j], a[n-i-1][j]);
}
}
void roty() {
for(int i=0; i<n; ++i) {
for(int j=0; j<m/2; ++j)
swap(a[i][j], a[i][m-j-1]);
}
}
bool check(int k) {
int col = -1;
for(int i=0; i<n; ++i) {
for(int j=0; j<m; ++j)
if(mx - a[i][j] > k) col = max(col, j);
for(int j=0; j<=col; ++j)
if(a[i][j] - mn > k) return false;
}
return true;
}
int run() {
int low = 0, high = 1e9;
while(low < high) {
int m = (low + high) / 2;
if(check(m))
high = m;
else
low = m+1;
}
return high;
}
void solve() {
cin >> n >> 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]);
}
}
int ans = run();
rotx();
ans = min(ans, run());
roty();
ans = min(ans, run());
rotx();
ans = min(ans, run());
cout << ans << '\n';
}
// CHECK YOUR OVERFLOWS!!!!
signed main() {
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin >> T;
for(int tc = 1; tc <= T; ++tc) {
// cout << "Case #" << tc << ": ";
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... |