#include<bits/stdc++.h>
using namespace std;
const int N=2005;
const int INF=1e9;
int n,m;
int a[N][N],lmx[N][N],rmx[N][N],lmn[N][N],rmn[N][N];
int mn=INF;
int bsearch(int i,int v){
int l=1,r=m;
while(l<r){
int m=(l+r)/2;
if(rmx[i][m]<=v)r=m;
else l=m+1;
}
return l;
}
bool check1(int d){
stack<pair<int,int>> s;
for(int i=1;i<=n;i++){
int p=upper_bound(lmx[i]+1,lmx[i]+m,mn+d)-lmx[i]-1;
while(!s.empty()&&s.top().second>p)s.pop();
s.emplace(i,p);
}
int lo=INF,hi=0,p;
for(int i=n;i>=1;i--){
if(!s.empty()&&s.top().first==i){
p=s.top().second;
s.pop();
}
lo=min(lo,rmn[i][p+1]);
hi=max(hi,rmx[i][p+1]);
if(hi-lo>d)return false;
}
return true;
}
bool check2(int d){
stack<pair<int,int>> s;
for(int i=1;i<=n;i++){
int p=bsearch(i,mn+d);
while(!s.empty()&&s.top().second<p)s.pop();
s.emplace(i,p);
}
int lo=INF,hi=0,p;
for(int i=n;i>=1;i--){
if(!s.empty()&&s.top().first==i){
p=s.top().second;
s.pop();
}
lo=min(lo,lmn[i][p-1]);
hi=max(hi,lmx[i][p-1]);
if(hi-lo>d)return false;
}
return true;
}
bool check3(int d){
stack<pair<int,int>> s;
for(int i=n;i>=1;i--){
int p=upper_bound(lmx[i]+1,lmx[i]+m,mn+d)-lmx[i]-1;
while(!s.empty()&&s.top().second>p)s.pop();
s.emplace(i,p);
}
int lo=INF,hi=0,p;
for(int i=1;i<=n;i++){
if(!s.empty()&&s.top().first==i){
p=s.top().second;
s.pop();
}
lo=min(lo,rmn[i][p+1]);
hi=max(hi,rmx[i][p+1]);
if(hi-lo>d)return false;
}
return true;
}
bool check4(int d){
stack<pair<int,int>> s;
for(int i=n;i>=1;i--){
int p=bsearch(i,mn+d);
while(!s.empty()&&s.top().second<p)s.pop();
s.emplace(i,p);
}
int lo=INF,hi=0,p;
for(int i=1;i<=n;i++){
if(!s.empty()&&s.top().first==i){
p=s.top().second;
s.pop();
}
lo=min(lo,lmn[i][p-1]);
hi=max(hi,lmx[i][p-1]);
if(hi-lo>d)return false;
}
return true;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin >> a[i][j],mn=min(mn,a[i][j]);
for(int i=1;i<=n;i++){
lmn[i][0]=rmn[i][m+1]=INF;
for(int j=1;j<=m;j++){
lmx[i][j]=max(a[i][j],lmx[i][j-1]);
lmn[i][j]=min(a[i][j],lmn[i][j-1]);
}
for(int j=m;j>=1;j--){
rmx[i][j]=max(a[i][j],rmx[i][j+1]);
rmn[i][j]=min(a[i][j],rmn[i][j+1]);
}
}
int l=0,r=INF;
while(l<r){
int m=(l+r)/2;
if(check1(m)||check2(m)||check3(m)||check4(m))r=m;
else l=m+1;
}
cout << l;
}
Compilation message
joioi.cpp: In function 'bool check3(int)':
joioi.cpp:75:27: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | lo=min(lo,rmn[i][p+1]);
| ~^~
joioi.cpp: In function 'bool check2(int)':
joioi.cpp:55:27: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
55 | lo=min(lo,lmn[i][p-1]);
| ~^~
joioi.cpp: In function 'bool check4(int)':
joioi.cpp:95:27: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
95 | lo=min(lo,lmn[i][p-1]);
| ~^~
joioi.cpp: In function 'bool check1(int)':
joioi.cpp:35:27: warning: 'p' may be used uninitialized in this function [-Wmaybe-uninitialized]
35 | lo=min(lo,rmn[i][p+1]);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4560 KB |
Output is correct |
3 |
Correct |
1 ms |
4560 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4560 KB |
Output is correct |
3 |
Correct |
1 ms |
4560 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4696 KB |
Output is correct |
16 |
Correct |
5 ms |
12636 KB |
Output is correct |
17 |
Correct |
6 ms |
12892 KB |
Output is correct |
18 |
Correct |
6 ms |
12892 KB |
Output is correct |
19 |
Correct |
6 ms |
12892 KB |
Output is correct |
20 |
Correct |
6 ms |
12632 KB |
Output is correct |
21 |
Correct |
8 ms |
12892 KB |
Output is correct |
22 |
Correct |
7 ms |
12888 KB |
Output is correct |
23 |
Correct |
7 ms |
13148 KB |
Output is correct |
24 |
Correct |
6 ms |
12636 KB |
Output is correct |
25 |
Correct |
7 ms |
12892 KB |
Output is correct |
26 |
Correct |
7 ms |
12892 KB |
Output is correct |
27 |
Correct |
7 ms |
13148 KB |
Output is correct |
28 |
Correct |
6 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4560 KB |
Output is correct |
3 |
Correct |
1 ms |
4560 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4696 KB |
Output is correct |
16 |
Correct |
5 ms |
12636 KB |
Output is correct |
17 |
Correct |
6 ms |
12892 KB |
Output is correct |
18 |
Correct |
6 ms |
12892 KB |
Output is correct |
19 |
Correct |
6 ms |
12892 KB |
Output is correct |
20 |
Correct |
6 ms |
12632 KB |
Output is correct |
21 |
Correct |
8 ms |
12892 KB |
Output is correct |
22 |
Correct |
7 ms |
12888 KB |
Output is correct |
23 |
Correct |
7 ms |
13148 KB |
Output is correct |
24 |
Correct |
6 ms |
12636 KB |
Output is correct |
25 |
Correct |
7 ms |
12892 KB |
Output is correct |
26 |
Correct |
7 ms |
12892 KB |
Output is correct |
27 |
Correct |
7 ms |
13148 KB |
Output is correct |
28 |
Correct |
6 ms |
12892 KB |
Output is correct |
29 |
Correct |
271 ms |
99540 KB |
Output is correct |
30 |
Correct |
265 ms |
100560 KB |
Output is correct |
31 |
Correct |
274 ms |
101908 KB |
Output is correct |
32 |
Correct |
281 ms |
101944 KB |
Output is correct |
33 |
Correct |
248 ms |
96180 KB |
Output is correct |
34 |
Correct |
273 ms |
101988 KB |
Output is correct |
35 |
Correct |
360 ms |
117696 KB |
Output is correct |
36 |
Correct |
312 ms |
112212 KB |
Output is correct |
37 |
Correct |
356 ms |
117864 KB |
Output is correct |