This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
const int N = 998244353;
const int INF = 1e9 + 7;
const int maxn = 2005;
ll a[maxn][maxn];
int main(void){
fastio;
int h, w;
cin>>h>>w;
ll mn = INF, mx = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin>>a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int ng = -1, ok = INF;
while(ng + 1 < ok){
int m = (ng + ok) / 2;
int bad = 0;
vector<int> mnx(h + 1, INF), mn2x(h + 1, INF);
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(a[i][j] > mn + m && a[i][j] < mx - m) bad = 1;
if(a[i][j] > mn + m) mnx[i] = min(mnx[i], j);
if(a[i][j] < mx - m) mn2x[i] = min(mn2x[i], j);
}
}
if(bad){
ng = m;
continue;
}
vector<int> lst(mn2x), lsb(mn2x), grt(mnx), grb(mnx);
for(int i = 1; i < h; i++) lst[i] = min(lst[i], lst[i - 1]), grt[i] = min(grt[i], grt[i - 1]);
for(int i = h - 2; i >= 0; i--) lsb[i] = min(lsb[i], lsb[i + 1]), grb[i] = min(grb[i], grb[i + 1]);
vector<int> gd(4, 1);
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(a[i][j] > mn + m){
if(j >= lst[i]) gd[0] = 0;
if(j >= lsb[i]) gd[1] = 0;
}
if(a[i][j] < mx - m){
if(j >= grt[i]) gd[2] = 0;
if(j >= grb[i]) gd[3] = 0;
}
}
}
if(gd[0] + gd[1] + gd[2] + gd[3]) ok = m;
else ng = m;
}
cout<<ok<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |