#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
int he[2005][2005];
int h,w;
vector<pair<int,int> > minn;
bool walked[2005][2005];
int dx[4] = {0,0,-1,1};
int Maxn,Minn;
int cnt;
int minnn = INT_MAX;
int maxnn = INT_MIN;
int minbound[2005];
int maxbound[2005];
bool check(int d){
// min on top
bool works=1;
for(int i=1;i<=h;i++){
minbound[i] = w+1;
maxbound[i] = 0;
}
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(he[i][j]<maxnn-d) break;
maxbound[i] = j;
}
for(int j=w;j>=1;j--){
if(he[i][j]>minnn+d) break;
minbound[i] = j;
}
if(minbound[i]-maxbound[i]>1){
works=0;
break;
}
}
if(works){
bool g = 1;
int k = 0;
for(int i=1;i<=h;i++){
k = max(k,minbound[i]);
if(maxbound[i]<k-1){g=0;break;}
}
if(g) return 1;
k = w+1;
g=1;
for(int i=1;i<=h;i++){
k = min(k,maxbound[i]);
if(minbound[i]>k+1){g=0;break;}
}
if(g) return 1;
}
// max on top
works=1;
for(int i=1;i<=h;i++){
minbound[i] = 0;
maxbound[i] = w;
}
for(int i=1;i<=h;i++){
for(int j=w;j>=1;j--){
if(he[i][j]<maxnn-d) break;
maxbound[i] = j;
}
for(int j=1;j<=w;j++){
if(he[i][j]>minnn+d) break;
minbound[i] = j;
}
if(maxbound[i]-minbound[i]>1){
works=0;
break;
}
}
if(works){
bool g = 1;
int k = 0;
for(int i=1;i<=h;i++){
k = max(k,maxbound[i]);
if(minbound[i]<k-1){g=0;break;}
}
if(g) return 1;
k = w+1;
g=1;
for(int i=1;i<=h;i++){
k = min(k,minbound[i]);
if(maxbound[i]>k+1){g=0;break;}
}
if(g) return 1;
}
return 0;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>h>>w;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>he[i][j];
minnn = min(minnn,he[i][j]);
maxnn = max(maxnn,he[i][j]);
}
}
int l = 0;int r = 1e9+5;
while(r-l>1){
int mid = (l+(r-l)/2);
if(check(mid))r = mid;
else l = mid;
}
cout<<r<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
3 ms |
4444 KB |
Output is correct |
17 |
Correct |
5 ms |
4952 KB |
Output is correct |
18 |
Correct |
4 ms |
4696 KB |
Output is correct |
19 |
Correct |
4 ms |
4700 KB |
Output is correct |
20 |
Correct |
4 ms |
4696 KB |
Output is correct |
21 |
Correct |
6 ms |
4932 KB |
Output is correct |
22 |
Correct |
5 ms |
4956 KB |
Output is correct |
23 |
Correct |
5 ms |
4956 KB |
Output is correct |
24 |
Correct |
4 ms |
4784 KB |
Output is correct |
25 |
Correct |
4 ms |
4832 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
5 ms |
4828 KB |
Output is correct |
28 |
Correct |
5 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
3 ms |
4444 KB |
Output is correct |
17 |
Correct |
5 ms |
4952 KB |
Output is correct |
18 |
Correct |
4 ms |
4696 KB |
Output is correct |
19 |
Correct |
4 ms |
4700 KB |
Output is correct |
20 |
Correct |
4 ms |
4696 KB |
Output is correct |
21 |
Correct |
6 ms |
4932 KB |
Output is correct |
22 |
Correct |
5 ms |
4956 KB |
Output is correct |
23 |
Correct |
5 ms |
4956 KB |
Output is correct |
24 |
Correct |
4 ms |
4784 KB |
Output is correct |
25 |
Correct |
4 ms |
4832 KB |
Output is correct |
26 |
Correct |
4 ms |
4956 KB |
Output is correct |
27 |
Correct |
5 ms |
4828 KB |
Output is correct |
28 |
Correct |
5 ms |
4956 KB |
Output is correct |
29 |
Correct |
296 ms |
35804 KB |
Output is correct |
30 |
Correct |
298 ms |
36368 KB |
Output is correct |
31 |
Correct |
314 ms |
37368 KB |
Output is correct |
32 |
Correct |
307 ms |
37584 KB |
Output is correct |
33 |
Correct |
253 ms |
33616 KB |
Output is correct |
34 |
Correct |
302 ms |
37524 KB |
Output is correct |
35 |
Correct |
344 ms |
50308 KB |
Output is correct |
36 |
Correct |
296 ms |
46104 KB |
Output is correct |
37 |
Correct |
348 ms |
50324 KB |
Output is correct |