#include<bits/stdc++.h>
using namespace std;
const int MX = 2005;
const int INF = 1e9;
int a[MX][MX];
int n, m;
int mn, mx;
int mark[MX][MX];
bool can(int ans, bool tag) {
for(int i=0; i<n; ++i) {
for(int j=0; j<m; ++j) {
mark[i][j] = -1;
if(a[i][j]-mn>ans) {
mark[i][j] = tag;
}
if(mx-a[i][j]>ans) {
if(mark[i][j]!=-1) return false;
mark[i][j] = tag^1;
}
}
}
auto LEFT_RIGHT_DIAGONAL = [&] () {
int prev = INF;
for(int i=0; i<n; ++i) {
int upto = m;
bool zero = false;
for(int j=m-1; j>-1; --j) {
if(mark[i][j]==0) {
zero = true;
if(j>prev) return false;
continue;
}
if(mark[i][j]==1) {
if(zero) return false;
upto = j;
}
}
prev = min(prev, upto-1);
}
return true;
};
auto RIGHT_LEFT_DIAGONAL = [&] () {
int prev = -1;
for(int i=0; i<n; ++i) {
int upto = -1;
bool one = false;
for(int j=0; j<m; ++j) {
if(mark[i][j]==1) {
one = true;
if(j<prev) return false;
continue;
}
if(mark[i][j]==0) {
if(one) return false;
upto = j;
}
}
prev = max(prev, upto+1);
}
return true;
};
return (LEFT_RIGHT_DIAGONAL()|(RIGHT_LEFT_DIAGONAL()));
}
void PlayGround() {
cin>>n>>m;
mx = -1, mn = INF;
for(int i=0; i<n; ++i) {
for(int j=0; j<m; ++j) {
cin>>a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int l = 0, r = 1e9;
while(l<r) {
int mid = (l+r)/2;
if(can(mid, 0) || can(mid, 1)) r = mid;
else l = mid+1;
}
cout<<l<<'\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
6 ms |
2308 KB |
Output is correct |
17 |
Correct |
9 ms |
2588 KB |
Output is correct |
18 |
Correct |
8 ms |
2516 KB |
Output is correct |
19 |
Correct |
11 ms |
2528 KB |
Output is correct |
20 |
Correct |
10 ms |
2260 KB |
Output is correct |
21 |
Correct |
10 ms |
2644 KB |
Output is correct |
22 |
Correct |
6 ms |
2516 KB |
Output is correct |
23 |
Correct |
12 ms |
2640 KB |
Output is correct |
24 |
Correct |
6 ms |
2388 KB |
Output is correct |
25 |
Correct |
16 ms |
2516 KB |
Output is correct |
26 |
Correct |
9 ms |
2644 KB |
Output is correct |
27 |
Correct |
10 ms |
2648 KB |
Output is correct |
28 |
Correct |
11 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
6 ms |
2308 KB |
Output is correct |
17 |
Correct |
9 ms |
2588 KB |
Output is correct |
18 |
Correct |
8 ms |
2516 KB |
Output is correct |
19 |
Correct |
11 ms |
2528 KB |
Output is correct |
20 |
Correct |
10 ms |
2260 KB |
Output is correct |
21 |
Correct |
10 ms |
2644 KB |
Output is correct |
22 |
Correct |
6 ms |
2516 KB |
Output is correct |
23 |
Correct |
12 ms |
2640 KB |
Output is correct |
24 |
Correct |
6 ms |
2388 KB |
Output is correct |
25 |
Correct |
16 ms |
2516 KB |
Output is correct |
26 |
Correct |
9 ms |
2644 KB |
Output is correct |
27 |
Correct |
10 ms |
2648 KB |
Output is correct |
28 |
Correct |
11 ms |
2644 KB |
Output is correct |
29 |
Correct |
780 ms |
52228 KB |
Output is correct |
30 |
Correct |
939 ms |
53324 KB |
Output is correct |
31 |
Correct |
1187 ms |
54828 KB |
Output is correct |
32 |
Correct |
607 ms |
54816 KB |
Output is correct |
33 |
Correct |
486 ms |
48024 KB |
Output is correct |
34 |
Correct |
938 ms |
54916 KB |
Output is correct |
35 |
Correct |
767 ms |
70440 KB |
Output is correct |
36 |
Correct |
431 ms |
65224 KB |
Output is correct |
37 |
Correct |
1304 ms |
70572 KB |
Output is correct |