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;
int h,w,a[2001][2001],s[2001][2001][2],mn[2001][2],mx[2001][2],lo=1e9,hi;
bool check(int x){
memset(s,0,sizeof(s));
for (int i=1;i<=h;i++){
mx[i][0]=mx[i][1]=-1;
mn[i][0]=mn[i][1]=1e9;
for (int j=1;j<=w;j++){
if (a[i][j]>lo+x&&a[i][j]<hi-x)
return false;
for (int k=0;k<2;k++)
s[i][j][k]=s[i][j-1][k]+s[i-1][j][k]-s[i-1][j-1][k];
if (a[i][j]>=hi-x&&a[i][j]<=lo+x)
continue;
int val=(a[i][j]>=hi-x);
s[i][j][val]++;
mx[i][val]=max(mx[i][val],j);
mn[i][val]=min(mn[i][val],j);
}
}
for (int k=0;k<2;k++)
for (int i=1;i<=h;i++){
int p=1e9,q=-1;
for (int j=i;j<=h;j++){
p=min(p,mn[j][k]);
q=max(q,mx[j][k]);
if (q==-1)
continue;
if ((s[i][q][k^1]-s[0][q][k^1]-s[i][p-1][k^1]+s[0][p-1][k^1])&&(s[h][q][k^1]-s[j-1][q][k^1]-s[h][p-1][k^1]+s[j-1][p-1][k^1]))
return false;
}
}
return true;
}
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> h >> w;
for (int i=1;i<=h;i++)
for (int j=1;j<=w;j++){
cin >> a[i][j];
lo=min(lo,a[i][j]);
hi=max(hi,a[i][j]);
}
int l=0,r=1e9,kq=-1;
while (l<=r){
int mid=(l+r)>>1;
if (check(mid)){
kq=mid;
r=mid-1;
}
else
l=mid+1;
}
cout << kq;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |