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 <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <iterator>
#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define bit __builtin_popcount
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
const ll INF=1e18+123;
const ld EPS=1e-9;
const int inf=1e9+123;
const int MOD=1e9+7;
const int N=2001;
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int a[N][N],m,n;
bool ioi[N][N];
bool check(int x){
memset(ioi,0,sizeof(ioi));
//---1
int mn=inf,mx=0,q=m;
for(int i=1;i<=n;i++){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x){
return 1;
}
//----2
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=1;i<=n;i++){
for(int j=m;j>=q;j--){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----3
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=m;
for(int i=n;i;i--){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----4
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=n;i;i--){
for(int j=m;j>=q;j--){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----5
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=n;
for(int i=1;i<=m;i++){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----6
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=1;i<=m;i++){
for(int j=n;j>=q;j--){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----7
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=n;
for(int i=m;i;i--){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----8
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=m;i;i--){
for(int j=n;j>=q;j--){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
return 0;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
int l=0,r=1e9;
int ans=0;
while(l<=r){
int mid=(l+r)/2;
if(!check(mid)){
l=mid+1;
}
else{
r=mid-1;
ans=mid;
}
}
printf("%d",ans);
return 0;
}
Compilation message (stderr)
joioi.cpp: In function 'int main()':
joioi.cpp:272:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
joioi.cpp:275:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i][j]);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |