#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];
bool chk(int x){
int l = mx - x,r = mx;
int mx1,mn1,mx2,mn2;
bool ok = 0;
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]);
}
}
int pt = m - 1;
mx1 = mx2 = -inf;
mn1 = mn2 = inf;
for(int i = 0;i < n;i++){
while(pt >= 0 && pref[i][pt] < l){
pt--;
}
for(int j = 0;j < m;j++){
if(j <= pt){
mx1 = max(mx1,v[i][j]);
mn1 = min(mn1,v[i][j]);
}else{
mx2 = max(mx2,v[i][j]);
mn2 = min(mn2,v[i][j]);
}
}
}
if(mx1 - mn1 <= x && mx2 - mn2 <= x){
ok = 1;
}
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];
}
}
}
return ok;
}
int main(){
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]);
}
}
int l = 0,r = inf;
while(l != r){
int mij = (l + r)/2;
if(chk(mij)){
r = mij;
}else l = mij + 1;
}
cout<<l<<'\n';
return 0;
}
Compilation message
joioi.cpp: In function 'bool chk(int)':
joioi.cpp:10:20: warning: unused variable 'r' [-Wunused-variable]
10 | int l = mx - x,r = mx;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2496 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2488 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2496 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2488 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
16 ms |
4444 KB |
Output is correct |
17 |
Correct |
22 ms |
5724 KB |
Output is correct |
18 |
Correct |
22 ms |
4440 KB |
Output is correct |
19 |
Correct |
22 ms |
4524 KB |
Output is correct |
20 |
Correct |
19 ms |
4444 KB |
Output is correct |
21 |
Correct |
22 ms |
5936 KB |
Output is correct |
22 |
Correct |
23 ms |
4696 KB |
Output is correct |
23 |
Correct |
26 ms |
3672 KB |
Output is correct |
24 |
Correct |
21 ms |
3416 KB |
Output is correct |
25 |
Correct |
30 ms |
3676 KB |
Output is correct |
26 |
Correct |
24 ms |
3676 KB |
Output is correct |
27 |
Correct |
32 ms |
3676 KB |
Output is correct |
28 |
Correct |
24 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2496 KB |
Output is correct |
6 |
Correct |
1 ms |
2540 KB |
Output is correct |
7 |
Correct |
1 ms |
2388 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2488 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
3932 KB |
Output is correct |
16 |
Correct |
16 ms |
4444 KB |
Output is correct |
17 |
Correct |
22 ms |
5724 KB |
Output is correct |
18 |
Correct |
22 ms |
4440 KB |
Output is correct |
19 |
Correct |
22 ms |
4524 KB |
Output is correct |
20 |
Correct |
19 ms |
4444 KB |
Output is correct |
21 |
Correct |
22 ms |
5936 KB |
Output is correct |
22 |
Correct |
23 ms |
4696 KB |
Output is correct |
23 |
Correct |
26 ms |
3672 KB |
Output is correct |
24 |
Correct |
21 ms |
3416 KB |
Output is correct |
25 |
Correct |
30 ms |
3676 KB |
Output is correct |
26 |
Correct |
24 ms |
3676 KB |
Output is correct |
27 |
Correct |
32 ms |
3676 KB |
Output is correct |
28 |
Correct |
24 ms |
3672 KB |
Output is correct |
29 |
Correct |
2130 ms |
69420 KB |
Output is correct |
30 |
Correct |
2028 ms |
68648 KB |
Output is correct |
31 |
Correct |
2102 ms |
70464 KB |
Output is correct |
32 |
Correct |
2154 ms |
70616 KB |
Output is correct |
33 |
Correct |
1967 ms |
66892 KB |
Output is correct |
34 |
Correct |
2128 ms |
70536 KB |
Output is correct |
35 |
Correct |
2333 ms |
86032 KB |
Output is correct |
36 |
Correct |
1987 ms |
80604 KB |
Output is correct |
37 |
Correct |
2374 ms |
86024 KB |
Output is correct |