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;
const int N = 2e3;
const int inf = 1e9;
int n,m,mx = -1;
int v[N][N];
int v2[N][N];
int pref[N][N];
bool chk(int x){
int l = mx - x,r = mx;
int mx1,mn1,mx2,mn2;
bool ok = 0;
for(int k = 0;k < 4;k++){
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
pref[i][j] = min((j == 0?inf:pref[i][j - 1]),v[i][j]);
}
}
int pt = m - 1;
mx1 = mx2 = -inf;
mn1 = mn2 = inf;
for(int i = 0;i < n;i++){
while(pt >= 0 && pref[i][pt] < l){
pt--;
}
for(int j = 0;j < m;j++){
if(j <= pt){
mx1 = max(mx1,v[i][j]);
mn1 = min(mn1,v[i][j]);
}else{
mx2 = max(mx2,v[i][j]);
mn2 = min(mn2,v[i][j]);
}
}
}
if(mx1 - mn1 <= x && mx2 - mn2 <= x){
ok = 1;
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
v2[j][n - i - 1] = v[i][j];
}
}
swap(n,m);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
v[i][j] = v2[i][j];
}
}
}
return ok;
}
int main(){
cin>>n>>m;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cin>>v[i][j];
mx = max(mx,v[i][j]);
}
}
int l = 0,r = inf;
while(l != r){
int mij = (l + r)/2;
if(chk(mij)){
r = mij;
}else l = mij + 1;
}
cout<<l<<'\n';
return 0;
}
Compilation message (stderr)
joioi.cpp: In function 'bool chk(int)':
joioi.cpp:10:20: warning: unused variable 'r' [-Wunused-variable]
10 | int l = mx - x,r = mx;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |