#include<bits/stdc++.h>
using namespace std;
const int N = 2e3;
const int inf = 1e9;
int n,m,mx = -1;
int v[N][N];
int v2[N][N];
int pref[N][N];
int suf[N][N];
int sufmx[N][N];
int ans = inf;
bool chk(int x){
bool ok = 0;
int pt = m - 1;
int mx2 = -inf,mn2 = inf;
for(int i = 0;i < n;i++){
while(pt >= 0 && pref[i][pt] < mx - x){
pt--;
}
if(pt != m - 1){
mx2 = max(sufmx[i][pt + 1],mx2);
mn2 = min(suf[i][pt + 1], mn2);
}
}
if(mx2 - mn2 <= x){
ok = 1;
}
return ok;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cin>>v[i][j];
mx = max(mx,v[i][j]);
}
}
ans = mx;
for(int k = 0;k < 4;k++){
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
pref[i][j] = min((j == 0?inf:pref[i][j - 1]),v[i][j]);
}
}
for(int i = 0;i < n;i++){
for(int j = m - 1;j >= 0;j--){
suf[i][j] = min((j == m - 1?inf:suf[i][j + 1]),v[i][j]);
sufmx[i][j] = max((j == m - 1?0:sufmx[i][j + 1]),v[i][j]);
}
}
int l = 0,r = ans;
while(l != r){
int mij = (l + r)/2;
if(chk(mij)){
r = mij;
}else l = mij + 1;
}
ans = min(ans,l);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
v2[j][n - i - 1] = v[i][j];
}
}
swap(n,m);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
v[i][j] = v2[i][j];
}
}
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
5 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5212 KB |
Output is correct |
19 |
Correct |
5 ms |
5212 KB |
Output is correct |
20 |
Correct |
6 ms |
5208 KB |
Output is correct |
21 |
Correct |
5 ms |
5212 KB |
Output is correct |
22 |
Correct |
5 ms |
5044 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5212 KB |
Output is correct |
25 |
Correct |
5 ms |
5212 KB |
Output is correct |
26 |
Correct |
8 ms |
5228 KB |
Output is correct |
27 |
Correct |
5 ms |
5464 KB |
Output is correct |
28 |
Correct |
5 ms |
5072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
3 ms |
5212 KB |
Output is correct |
17 |
Correct |
5 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5212 KB |
Output is correct |
19 |
Correct |
5 ms |
5212 KB |
Output is correct |
20 |
Correct |
6 ms |
5208 KB |
Output is correct |
21 |
Correct |
5 ms |
5212 KB |
Output is correct |
22 |
Correct |
5 ms |
5044 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5212 KB |
Output is correct |
25 |
Correct |
5 ms |
5212 KB |
Output is correct |
26 |
Correct |
8 ms |
5228 KB |
Output is correct |
27 |
Correct |
5 ms |
5464 KB |
Output is correct |
28 |
Correct |
5 ms |
5072 KB |
Output is correct |
29 |
Correct |
272 ms |
78520 KB |
Output is correct |
30 |
Correct |
266 ms |
78472 KB |
Output is correct |
31 |
Correct |
298 ms |
78676 KB |
Output is correct |
32 |
Correct |
292 ms |
78556 KB |
Output is correct |
33 |
Correct |
277 ms |
78108 KB |
Output is correct |
34 |
Correct |
332 ms |
78676 KB |
Output is correct |
35 |
Correct |
369 ms |
78632 KB |
Output is correct |
36 |
Correct |
286 ms |
78160 KB |
Output is correct |
37 |
Correct |
330 ms |
78596 KB |
Output is correct |