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 pii pair<int,int>
#define pll pair<ll,ll>
#define ff first
#define ss second
using namespace std;
const int N=2010;
int n,m,ans,a[N][N],b[N][N],mn;
void turn(){
for(int i=1; i<=n; i++) for(int j = 1; j<=m; j++)b[j][n+1-i]=a[i][j];
swap(n,m);
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)a[i][j]=b[i][j];
}
bool check(int o){
for(int k=0; k<4; k++){
int lst=m,omx=0,omn=1e9;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= lst; j++)if(a[i][j]>mn+o){lst=j-1; break;}
for(int j=lst+1; j<=m; j++){
omx=max(omx,a[i][j]);
omn=min(omn,a[i][j]);
}
}
if(omx-omn<=o)return 1;
if(k<3) turn();
}
return 0;
}
int main() {
cin>>n>>m;
mn=1e9;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++){cin>>a[i][j]; mn=min(mn,a[i][j]);}
int l=0,r=1e9;
while(l<=r){
int md=(l+r)/2;
if(check(md))r=md-1;
else l=md+1;
}
cout<<l;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |