#include <bits/stdc++.h>
using namespace std;
int n, m, mini = 2e9, maxi = 0, a[2007][2007], sv[2007][2007];
int read(){
char p;
while((p = getchar())){
if(p < '0' || p > '9') continue;
break;
}
int ret = p - '0';
while((p = getchar())){
if(p >= '0' && p <= '9'){
ret *= 10;
ret += p - '0';
}
else break;
}
return ret;
}
bool Check(int x, int n, int m){
int cur = m, mi = 2e9, mx = 0;
bool ok = true;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= cur; j++){
if(sv[i][j] - mini > x){
cur = j - 1;
break;
}
}
for(int j = cur + 1; j <= m; j++){
mi = min(mi, sv[i][j]), mx = max(mx, sv[i][j]);
}
if(mx - mi > x){
ok = false;
break;
}
}
if(ok) return true;
cur = 1, mi = 2e9, mx = 0;
for(int i = 1; i <= n; i++){
for(int j = m; j >= cur; j--){
if(sv[i][j] - mini > x){
cur = j + 1;
break;
}
}
for(int j = cur - 1; j >= 1; j--){
mi = min(mi, sv[i][j]), mx = max(mx, sv[i][j]);
}
if(mx - mi > x) return false;
}
return true;
}
bool check(int x){
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) sv[i][j] = a[i][j];
if(Check(x, n, m)) return true;
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) sv[i][j] = a[n - i + 1][j];
if(Check(x, n, m)) return true;
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) sv[j][i] = a[i][j];
if(Check(x, m, n)) return true;
for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) sv[j][i] = a[i][m - j + 1];
if(Check(x, m, n)) return true;
return false;
}
int main(){
n = read(), m = read();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
a[i][j] = read();
mini = min(mini, a[i][j]);
maxi = max(maxi, a[i][j]);
}
}
int l = 0, r = maxi, mid = (l + r) / 2;
while(l < r){
if(check(mid)) r = mid;
else l = mid + 1;
mid = (l + r) / 2;
}
printf("%d", mid);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
3 ms |
660 KB |
Output is correct |
6 |
Correct |
2 ms |
660 KB |
Output is correct |
7 |
Correct |
2 ms |
660 KB |
Output is correct |
8 |
Correct |
6 ms |
660 KB |
Output is correct |
9 |
Correct |
2 ms |
660 KB |
Output is correct |
10 |
Correct |
2 ms |
660 KB |
Output is correct |
11 |
Correct |
2 ms |
660 KB |
Output is correct |
12 |
Correct |
2 ms |
740 KB |
Output is correct |
13 |
Correct |
2 ms |
740 KB |
Output is correct |
14 |
Correct |
2 ms |
740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
3 ms |
660 KB |
Output is correct |
6 |
Correct |
2 ms |
660 KB |
Output is correct |
7 |
Correct |
2 ms |
660 KB |
Output is correct |
8 |
Correct |
6 ms |
660 KB |
Output is correct |
9 |
Correct |
2 ms |
660 KB |
Output is correct |
10 |
Correct |
2 ms |
660 KB |
Output is correct |
11 |
Correct |
2 ms |
660 KB |
Output is correct |
12 |
Correct |
2 ms |
740 KB |
Output is correct |
13 |
Correct |
2 ms |
740 KB |
Output is correct |
14 |
Correct |
2 ms |
740 KB |
Output is correct |
15 |
Correct |
3 ms |
1396 KB |
Output is correct |
16 |
Correct |
6 ms |
2556 KB |
Output is correct |
17 |
Correct |
12 ms |
2792 KB |
Output is correct |
18 |
Correct |
10 ms |
3416 KB |
Output is correct |
19 |
Correct |
10 ms |
3436 KB |
Output is correct |
20 |
Correct |
9 ms |
3580 KB |
Output is correct |
21 |
Correct |
16 ms |
4076 KB |
Output is correct |
22 |
Correct |
14 ms |
4352 KB |
Output is correct |
23 |
Correct |
13 ms |
4864 KB |
Output is correct |
24 |
Correct |
14 ms |
5012 KB |
Output is correct |
25 |
Correct |
14 ms |
5612 KB |
Output is correct |
26 |
Correct |
13 ms |
6000 KB |
Output is correct |
27 |
Correct |
14 ms |
6388 KB |
Output is correct |
28 |
Correct |
17 ms |
6904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
576 KB |
Output is correct |
4 |
Correct |
2 ms |
576 KB |
Output is correct |
5 |
Correct |
3 ms |
660 KB |
Output is correct |
6 |
Correct |
2 ms |
660 KB |
Output is correct |
7 |
Correct |
2 ms |
660 KB |
Output is correct |
8 |
Correct |
6 ms |
660 KB |
Output is correct |
9 |
Correct |
2 ms |
660 KB |
Output is correct |
10 |
Correct |
2 ms |
660 KB |
Output is correct |
11 |
Correct |
2 ms |
660 KB |
Output is correct |
12 |
Correct |
2 ms |
740 KB |
Output is correct |
13 |
Correct |
2 ms |
740 KB |
Output is correct |
14 |
Correct |
2 ms |
740 KB |
Output is correct |
15 |
Correct |
3 ms |
1396 KB |
Output is correct |
16 |
Correct |
6 ms |
2556 KB |
Output is correct |
17 |
Correct |
12 ms |
2792 KB |
Output is correct |
18 |
Correct |
10 ms |
3416 KB |
Output is correct |
19 |
Correct |
10 ms |
3436 KB |
Output is correct |
20 |
Correct |
9 ms |
3580 KB |
Output is correct |
21 |
Correct |
16 ms |
4076 KB |
Output is correct |
22 |
Correct |
14 ms |
4352 KB |
Output is correct |
23 |
Correct |
13 ms |
4864 KB |
Output is correct |
24 |
Correct |
14 ms |
5012 KB |
Output is correct |
25 |
Correct |
14 ms |
5612 KB |
Output is correct |
26 |
Correct |
13 ms |
6000 KB |
Output is correct |
27 |
Correct |
14 ms |
6388 KB |
Output is correct |
28 |
Correct |
17 ms |
6904 KB |
Output is correct |
29 |
Correct |
1082 ms |
57548 KB |
Output is correct |
30 |
Correct |
1084 ms |
79892 KB |
Output is correct |
31 |
Correct |
1053 ms |
93832 KB |
Output is correct |
32 |
Correct |
1277 ms |
93864 KB |
Output is correct |
33 |
Correct |
992 ms |
93864 KB |
Output is correct |
34 |
Correct |
1084 ms |
93864 KB |
Output is correct |
35 |
Correct |
1967 ms |
93864 KB |
Output is correct |
36 |
Correct |
1454 ms |
93864 KB |
Output is correct |
37 |
Correct |
1694 ms |
93864 KB |
Output is correct |