#include <bits/stdc++.h>
using namespace std;
int h,w,a[2006][2006],mn[2006][2],mx[2006][2],tmp[2006],lo=1e9,hi;
bool check(int x){
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;
if (a[i][j]>=hi-x&&a[i][j]<=lo+x)
continue;
int val=(a[i][j]>=hi-x);
mx[i][val]=max(mx[i][val],j);
mn[i][val]=min(mn[i][val],j);
}
}
int ch=4;
tmp[h+1]=0;
for (int k=0;k<2;k++){
for (int i=1;i<=h;i++)
tmp[i]=max(tmp[i-1],mx[i][k]);
int cur=w+1;
for (int i=h;i>=1;i--){
cur=min(cur,mn[i][k^1]);
if (cur<=tmp[i]){
ch--;
break;
}
}
for (int i=h;i>=1;i--)
tmp[i]=max(tmp[i+1],mx[i][k]);
cur=w+1;
for (int i=1;i<=h;i++){
cur=min(cur,mn[i][k^1]);
if (cur<=tmp[i]){
ch--;
break;
}
}
}
return ch;
}
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 |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
17 |
Correct |
5 ms |
2652 KB |
Output is correct |
18 |
Correct |
5 ms |
2652 KB |
Output is correct |
19 |
Correct |
7 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2652 KB |
Output is correct |
21 |
Correct |
7 ms |
2980 KB |
Output is correct |
22 |
Correct |
6 ms |
2996 KB |
Output is correct |
23 |
Correct |
8 ms |
2908 KB |
Output is correct |
24 |
Correct |
5 ms |
2964 KB |
Output is correct |
25 |
Correct |
11 ms |
2968 KB |
Output is correct |
26 |
Correct |
6 ms |
2996 KB |
Output is correct |
27 |
Correct |
7 ms |
2908 KB |
Output is correct |
28 |
Correct |
9 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
2652 KB |
Output is correct |
17 |
Correct |
5 ms |
2652 KB |
Output is correct |
18 |
Correct |
5 ms |
2652 KB |
Output is correct |
19 |
Correct |
7 ms |
2652 KB |
Output is correct |
20 |
Correct |
5 ms |
2652 KB |
Output is correct |
21 |
Correct |
7 ms |
2980 KB |
Output is correct |
22 |
Correct |
6 ms |
2996 KB |
Output is correct |
23 |
Correct |
8 ms |
2908 KB |
Output is correct |
24 |
Correct |
5 ms |
2964 KB |
Output is correct |
25 |
Correct |
11 ms |
2968 KB |
Output is correct |
26 |
Correct |
6 ms |
2996 KB |
Output is correct |
27 |
Correct |
7 ms |
2908 KB |
Output is correct |
28 |
Correct |
9 ms |
2908 KB |
Output is correct |
29 |
Correct |
420 ms |
37436 KB |
Output is correct |
30 |
Correct |
449 ms |
37656 KB |
Output is correct |
31 |
Correct |
583 ms |
39492 KB |
Output is correct |
32 |
Correct |
382 ms |
39228 KB |
Output is correct |
33 |
Correct |
298 ms |
35140 KB |
Output is correct |
34 |
Correct |
527 ms |
39248 KB |
Output is correct |
35 |
Correct |
530 ms |
54860 KB |
Output is correct |
36 |
Correct |
464 ms |
49736 KB |
Output is correct |
37 |
Correct |
712 ms |
54972 KB |
Output is correct |