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>
#define ll long long
#define pll pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
int a[2005][2005]{0},mx=0,mn=1e9,h,w;
bool ch(ll x){
int id=w+1;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(mx-a[i][j]>x)id=min(id,j);
}
for(int j=id;j<=w;j++){
if(a[i][j]-mn>x)return 0;
}
}return 1;
}
ll solve(){
ll l=0,r=mx-mn;
while(l<r){
ll m=(l+r)>>1;
if(ch(m))r=m;
else l=m+1;
}return l;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>h>>w;
for(int i=1;i<=h;i++)for(int j=1;j<=w;j++)cin>>a[i][j],mx=max(mx,a[i][j]),mn=min(mn,a[i][j]);
ll ans=1e9;
ans=min(solve(),ans);
for(int i=1;i<=h;i++)for(int j=1;j<=w/2;j++)swap(a[i][j],a[i][w-j+1]);
ans=min(solve(),ans);
for(int j=1;j<=w;j++)for(int i=1;i<=h/2;i++)swap(a[i][j],a[h-i+1][j]);
ans=min(solve(),ans);
for(int i=1;i<=h;i++)for(int j=1;j<=w/2;j++)swap(a[i][j],a[i][w-j+1]);
ans=min(solve(),ans);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |