#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e3+7, INF=1e9+7;
int A[LIM][LIM], T[LIM][LIM], n, m;
bool f(int x) {
int ma=0, mi=INF;
rep(i, n) rep(j, m) {
ma=max(ma, A[i][j]);
mi=min(mi, A[i][j]);
}
rep(i, n) rep(j, m) {
T[i][j]=0;
if(A[i][j]-mi<=x) ++T[i][j];
if(ma-A[i][j]<=x) T[i][j]+=2;
if(!T[i][j]) return false;
}
bool ok=true;
int lst=m;
rep(i, n) {
int akt=0;
while(akt<lst && T[i][akt]!=1) ++akt;
lst=akt;
for(int j=m-1; j>=akt; --j) if(T[i][j]==2) ok=false;
}
if(ok) return true;
ok=true;
lst=m;
rep(i, n) {
int akt=0;
while(akt<lst && T[i][akt]!=2) ++akt;
lst=akt;
for(int j=m-1; j>=akt; --j) if(T[i][j]==1) ok=false;
}
if(ok) return true;
ok=true;
lst=-1;
rep(i, n) {
int akt=m-1;
while(akt>lst && T[i][akt]!=1) --akt;
lst=akt;
for(int j=akt; j>=0; --j) if(T[i][j]==2) ok=false;
}
if(ok) return true;
ok=true;
lst=-1;
rep(i, n) {
int akt=m-1;
while(akt>lst && T[i][akt]!=2) --akt;
lst=akt;
for(int j=akt; j>=0; --j) if(T[i][j]==1) ok=false;
}
return ok;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep(i, n) rep(j, m) cin >> A[i][j];
int p=0, k=INF;
while(p<k) {
int sr=(p+k)/2;
if(f(sr)) k=sr; else p=sr+1;
}
cout << p << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
7 ms |
2252 KB |
Output is correct |
17 |
Correct |
8 ms |
2372 KB |
Output is correct |
18 |
Correct |
9 ms |
2384 KB |
Output is correct |
19 |
Correct |
11 ms |
2376 KB |
Output is correct |
20 |
Correct |
9 ms |
2252 KB |
Output is correct |
21 |
Correct |
11 ms |
2500 KB |
Output is correct |
22 |
Correct |
7 ms |
2508 KB |
Output is correct |
23 |
Correct |
13 ms |
2512 KB |
Output is correct |
24 |
Correct |
7 ms |
2344 KB |
Output is correct |
25 |
Correct |
14 ms |
2568 KB |
Output is correct |
26 |
Correct |
11 ms |
2500 KB |
Output is correct |
27 |
Correct |
11 ms |
2556 KB |
Output is correct |
28 |
Correct |
13 ms |
2556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
7 ms |
2252 KB |
Output is correct |
17 |
Correct |
8 ms |
2372 KB |
Output is correct |
18 |
Correct |
9 ms |
2384 KB |
Output is correct |
19 |
Correct |
11 ms |
2376 KB |
Output is correct |
20 |
Correct |
9 ms |
2252 KB |
Output is correct |
21 |
Correct |
11 ms |
2500 KB |
Output is correct |
22 |
Correct |
7 ms |
2508 KB |
Output is correct |
23 |
Correct |
13 ms |
2512 KB |
Output is correct |
24 |
Correct |
7 ms |
2344 KB |
Output is correct |
25 |
Correct |
14 ms |
2568 KB |
Output is correct |
26 |
Correct |
11 ms |
2500 KB |
Output is correct |
27 |
Correct |
11 ms |
2556 KB |
Output is correct |
28 |
Correct |
13 ms |
2556 KB |
Output is correct |
29 |
Correct |
771 ms |
52184 KB |
Output is correct |
30 |
Correct |
774 ms |
53136 KB |
Output is correct |
31 |
Correct |
888 ms |
46000 KB |
Output is correct |
32 |
Correct |
782 ms |
47044 KB |
Output is correct |
33 |
Correct |
529 ms |
47828 KB |
Output is correct |
34 |
Correct |
837 ms |
54876 KB |
Output is correct |
35 |
Correct |
999 ms |
70336 KB |
Output is correct |
36 |
Correct |
756 ms |
65172 KB |
Output is correct |
37 |
Correct |
1092 ms |
70504 KB |
Output is correct |