#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
const int N = 998244353;
const int INF = 1e9 + 7;
const int maxn = 2005;
ll a[maxn][maxn];
int main(void){
fastio;
int h, w;
cin>>h>>w;
ll mn = INF, mx = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin>>a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int ng = -1, ok = INF;
while(ng + 1 < ok){
int m = (ng + ok) / 2;
int bad = 0;
vector<int> mnx(h + 1, INF), mn2x(h + 1, INF);
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(a[i][j] > mn + m && a[i][j] < mx - m) bad = 1;
if(a[i][j] > mn + m) mnx[i] = min(mnx[i], j);
if(a[i][j] < mx - m) mn2x[i] = min(mn2x[i], j);
}
}
if(bad){
ng = m;
continue;
}
vector<int> lst(mn2x), lsb(mn2x), grt(mnx), grb(mnx);
for(int i = 1; i < h; i++) lst[i] = min(lst[i], lst[i - 1]), grt[i] = min(grt[i], grt[i - 1]);
for(int i = h - 2; i >= 0; i--) lsb[i] = min(lsb[i], lsb[i + 1]), grb[i] = min(grb[i], grb[i + 1]);
vector<int> gd(4, 1);
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
if(a[i][j] > mn + m){
if(j >= lst[i]) gd[0] = 0;
if(j >= lsb[i]) gd[1] = 0;
}
if(a[i][j] < mx - m){
if(j >= grt[i]) gd[2] = 0;
if(j >= grb[i]) gd[3] = 0;
}
}
}
if(gd[0] + gd[1] + gd[2] + gd[3]) ok = m;
else ng = m;
}
cout<<ok<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
7 ms |
4700 KB |
Output is correct |
17 |
Correct |
6 ms |
4696 KB |
Output is correct |
18 |
Correct |
6 ms |
4900 KB |
Output is correct |
19 |
Correct |
9 ms |
4700 KB |
Output is correct |
20 |
Correct |
9 ms |
4700 KB |
Output is correct |
21 |
Correct |
8 ms |
5020 KB |
Output is correct |
22 |
Correct |
7 ms |
4952 KB |
Output is correct |
23 |
Correct |
11 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4976 KB |
Output is correct |
25 |
Correct |
15 ms |
4952 KB |
Output is correct |
26 |
Correct |
8 ms |
4956 KB |
Output is correct |
27 |
Correct |
8 ms |
4952 KB |
Output is correct |
28 |
Correct |
8 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
7 ms |
4700 KB |
Output is correct |
17 |
Correct |
6 ms |
4696 KB |
Output is correct |
18 |
Correct |
6 ms |
4900 KB |
Output is correct |
19 |
Correct |
9 ms |
4700 KB |
Output is correct |
20 |
Correct |
9 ms |
4700 KB |
Output is correct |
21 |
Correct |
8 ms |
5020 KB |
Output is correct |
22 |
Correct |
7 ms |
4952 KB |
Output is correct |
23 |
Correct |
11 ms |
4956 KB |
Output is correct |
24 |
Correct |
5 ms |
4976 KB |
Output is correct |
25 |
Correct |
15 ms |
4952 KB |
Output is correct |
26 |
Correct |
8 ms |
4956 KB |
Output is correct |
27 |
Correct |
8 ms |
4952 KB |
Output is correct |
28 |
Correct |
8 ms |
4992 KB |
Output is correct |
29 |
Correct |
661 ms |
53404 KB |
Output is correct |
30 |
Correct |
778 ms |
53408 KB |
Output is correct |
31 |
Correct |
969 ms |
55004 KB |
Output is correct |
32 |
Correct |
587 ms |
54988 KB |
Output is correct |
33 |
Correct |
471 ms |
49440 KB |
Output is correct |
34 |
Correct |
848 ms |
55236 KB |
Output is correct |
35 |
Correct |
709 ms |
70644 KB |
Output is correct |
36 |
Correct |
616 ms |
65412 KB |
Output is correct |
37 |
Correct |
1168 ms |
71004 KB |
Output is correct |