#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
33368 KB |
Output is correct |
2 |
Correct |
61 ms |
33368 KB |
Output is correct |
3 |
Correct |
61 ms |
33372 KB |
Output is correct |
4 |
Correct |
63 ms |
33368 KB |
Output is correct |
5 |
Correct |
62 ms |
33372 KB |
Output is correct |
6 |
Correct |
59 ms |
33624 KB |
Output is correct |
7 |
Correct |
61 ms |
33368 KB |
Output is correct |
8 |
Correct |
62 ms |
33372 KB |
Output is correct |
9 |
Incorrect |
62 ms |
33372 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
33368 KB |
Output is correct |
2 |
Correct |
61 ms |
33368 KB |
Output is correct |
3 |
Correct |
61 ms |
33372 KB |
Output is correct |
4 |
Correct |
63 ms |
33368 KB |
Output is correct |
5 |
Correct |
62 ms |
33372 KB |
Output is correct |
6 |
Correct |
59 ms |
33624 KB |
Output is correct |
7 |
Correct |
61 ms |
33368 KB |
Output is correct |
8 |
Correct |
62 ms |
33372 KB |
Output is correct |
9 |
Incorrect |
62 ms |
33372 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
33368 KB |
Output is correct |
2 |
Correct |
61 ms |
33368 KB |
Output is correct |
3 |
Correct |
61 ms |
33372 KB |
Output is correct |
4 |
Correct |
63 ms |
33368 KB |
Output is correct |
5 |
Correct |
62 ms |
33372 KB |
Output is correct |
6 |
Correct |
59 ms |
33624 KB |
Output is correct |
7 |
Correct |
61 ms |
33368 KB |
Output is correct |
8 |
Correct |
62 ms |
33372 KB |
Output is correct |
9 |
Incorrect |
62 ms |
33372 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |