#include<iostream>
#include<stdio.h>
using namespace std;
int h,w;
int A[2000][2000];
int M,m;
bool test(int x){//cout<<x<<endl;
int region[h][w];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++)region[i][j]=-1;
}
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(A[i][j]>m+x){
if(region[i][j]==-1)region[i][j]=1;
else return false;
}
if(A[i][j]<M-x){
if(region[i][j]==-1)region[i][j]=0;
else return false;
}//cout<<region[i][j]<<" ";
}
}
/*for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cout<<region[i][j]<<" ";
}cout<<endl;
}cout<<endl;*/
int loH[h+w][2];
int hiH[h+w][2];
//compute hi
for(int i=0;i<h;i++){
hiH[i][0]=-1;
hiH[i][1]=-1;
for(int j=0;j<w;j++){
if(region[i][j]==0)hiH[i][0]=j;
if(region[i][j]==1)hiH[i][1]=j;
}
//cout<<hiH[i][0]<<" "<<hiH[i][1]<<endl;
}
//compute lo
for(int i=0;i<h;i++){
loH[i][0]=w;
loH[i][1]=w;
for(int j=w-1;j>-1;j--){
if(region[i][j]==0)loH[i][0]=j;
if(region[i][j]==1)loH[i][1]=j;
}
//cout<<loH[i][0]<<" "<<loH[i][1]<<endl;
}
int Hi;
bool b;
Hi=-1;
b=true;
for(int i=0;i<h;i++){
Hi=max(Hi,hiH[i][0]);
if(Hi>=loH[i][1])b=false;
}
if(b)return true;
b=true;
Hi=-1;
for(int i=0;i<h;i++){
Hi=max(Hi,hiH[i][1]);
if(Hi>=loH[i][0])b=false;
}
if(b)return true;
Hi=-1;
b=true;
for(int i=h-1;i>-1;i--){
Hi=max(Hi,hiH[i][0]);
if(Hi>=loH[i][1])b=false;
}
if(b)return true;
b=true;
Hi=-1;
for(int i=h-1;i>-1;i--){
Hi=max(Hi,hiH[i][1]);
if(Hi>=loH[i][0])b=false;
}
if(b)return true;
//compute hi
for(int i=0;i<w;i++){
hiH[i][0]=-1;
hiH[i][1]=-1;
for(int j=0;j<h;j++){
if(region[j][i]==0)hiH[i][0]=j;
if(region[j][i]==1)hiH[i][1]=j;
}
}
//compute lo
for(int i=0;i<w;i++){
loH[i][0]=h;
loH[i][1]=h;
for(int j=h-1;j>-1;j--){
if(region[j][i]==0)loH[i][0]=j;
if(region[j][i]==1)loH[i][1]=j;
}
}
Hi=-1;
b=true;
for(int i=0;i<w;i++){
Hi=max(Hi,hiH[i][0]);
if(Hi>=loH[i][1])b=false;
}
if(b)return true;
b=true;
Hi=-1;
for(int i=0;i<w;i++){
Hi=max(Hi,hiH[i][1]);
if(Hi>=loH[i][0])b=false;
}
if(b)return true;
Hi=-1;
b=true;
for(int i=w-1;i>-1;i--){
Hi=max(Hi,hiH[i][0]);
if(Hi>=loH[i][1])b=false;
}
if(b)return true;
b=true;
Hi=-1;
for(int i=w-1;i>-1;i--){
Hi=max(Hi,hiH[i][1]);
if(Hi>=loH[i][0])b=false;
}
if(b)return true;
return false;
}
int main(){
cin>>h>>w;
M=-1;
m=1000000001;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>A[i][j];
M=max(M,A[i][j]);
m=min(m,A[i][j]);
}
}
int hi,lo;
lo=-1;
hi=M-m;
while(hi-lo>1){
int mid=(hi+lo)/2;
bool b=test(mid);
if(b)hi=mid;
else lo=mid;
}cout<<hi<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
4 ms |
488 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
560 KB |
Output is correct |
6 |
Correct |
2 ms |
780 KB |
Output is correct |
7 |
Correct |
2 ms |
780 KB |
Output is correct |
8 |
Correct |
3 ms |
780 KB |
Output is correct |
9 |
Correct |
4 ms |
780 KB |
Output is correct |
10 |
Correct |
3 ms |
780 KB |
Output is correct |
11 |
Correct |
2 ms |
780 KB |
Output is correct |
12 |
Correct |
3 ms |
780 KB |
Output is correct |
13 |
Correct |
3 ms |
780 KB |
Output is correct |
14 |
Correct |
3 ms |
780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
4 ms |
488 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
560 KB |
Output is correct |
6 |
Correct |
2 ms |
780 KB |
Output is correct |
7 |
Correct |
2 ms |
780 KB |
Output is correct |
8 |
Correct |
3 ms |
780 KB |
Output is correct |
9 |
Correct |
4 ms |
780 KB |
Output is correct |
10 |
Correct |
3 ms |
780 KB |
Output is correct |
11 |
Correct |
2 ms |
780 KB |
Output is correct |
12 |
Correct |
3 ms |
780 KB |
Output is correct |
13 |
Correct |
3 ms |
780 KB |
Output is correct |
14 |
Correct |
3 ms |
780 KB |
Output is correct |
15 |
Correct |
4 ms |
780 KB |
Output is correct |
16 |
Correct |
22 ms |
1932 KB |
Output is correct |
17 |
Correct |
35 ms |
2280 KB |
Output is correct |
18 |
Correct |
24 ms |
2440 KB |
Output is correct |
19 |
Correct |
38 ms |
2752 KB |
Output is correct |
20 |
Correct |
38 ms |
2800 KB |
Output is correct |
21 |
Correct |
46 ms |
3348 KB |
Output is correct |
22 |
Correct |
34 ms |
3752 KB |
Output is correct |
23 |
Correct |
49 ms |
4136 KB |
Output is correct |
24 |
Correct |
26 ms |
4412 KB |
Output is correct |
25 |
Correct |
72 ms |
4884 KB |
Output is correct |
26 |
Correct |
50 ms |
5272 KB |
Output is correct |
27 |
Correct |
44 ms |
5700 KB |
Output is correct |
28 |
Correct |
39 ms |
6048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
4 ms |
488 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
560 KB |
Output is correct |
6 |
Correct |
2 ms |
780 KB |
Output is correct |
7 |
Correct |
2 ms |
780 KB |
Output is correct |
8 |
Correct |
3 ms |
780 KB |
Output is correct |
9 |
Correct |
4 ms |
780 KB |
Output is correct |
10 |
Correct |
3 ms |
780 KB |
Output is correct |
11 |
Correct |
2 ms |
780 KB |
Output is correct |
12 |
Correct |
3 ms |
780 KB |
Output is correct |
13 |
Correct |
3 ms |
780 KB |
Output is correct |
14 |
Correct |
3 ms |
780 KB |
Output is correct |
15 |
Correct |
4 ms |
780 KB |
Output is correct |
16 |
Correct |
22 ms |
1932 KB |
Output is correct |
17 |
Correct |
35 ms |
2280 KB |
Output is correct |
18 |
Correct |
24 ms |
2440 KB |
Output is correct |
19 |
Correct |
38 ms |
2752 KB |
Output is correct |
20 |
Correct |
38 ms |
2800 KB |
Output is correct |
21 |
Correct |
46 ms |
3348 KB |
Output is correct |
22 |
Correct |
34 ms |
3752 KB |
Output is correct |
23 |
Correct |
49 ms |
4136 KB |
Output is correct |
24 |
Correct |
26 ms |
4412 KB |
Output is correct |
25 |
Correct |
72 ms |
4884 KB |
Output is correct |
26 |
Correct |
50 ms |
5272 KB |
Output is correct |
27 |
Correct |
44 ms |
5700 KB |
Output is correct |
28 |
Correct |
39 ms |
6048 KB |
Output is correct |
29 |
Correct |
3057 ms |
56920 KB |
Output is correct |
30 |
Correct |
3421 ms |
78968 KB |
Output is correct |
31 |
Execution timed out |
4030 ms |
103240 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |