#include <bits/stdc++.h>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emaplce_back
#define MP make_pair
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
const int INF = 1e9 + 7, N = 2003;
int a[N][N];
signed main()
{
IO_OP;
int h, w;
cin >> h >> w;
int mx = 0, mn = INF;
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
}
auto ok = [&] (int max_ans) {
auto must_with_mx = [&] (int x) {
return x - mn > max_ans;
};
auto must_with_mn = [&] (int x) {
return mx - x > max_ans;
};
auto yes1 = [&]() {
V<vi> b(h, vi(w));
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
if(must_with_mx(a[i][j]))
b[i][j] = 1;
for(int i = 0; i < h; i++)
for(int j = w - 1; j >= 0; j--) {
if(i + 1 < h) b[i + 1][j] |= b[i][j];
if(j - 1 >= 0) b[i][j - 1] |= b[i][j];
if(b[i][j] && must_with_mn(a[i][j]))
return false;
}
return true;
};
auto yes2 = [&]() {
V<vi> b(h, vi(w));
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
if(must_with_mx(a[i][j]))
b[i][j] = 1;
for(int i = h - 1; i >= 0; i--)
for(int j = w - 1; j >= 0; j--) {
if(i - 1 >= 0) b[i - 1][j] |= b[i][j];
if(j - 1 >= 0) b[i][j - 1] |= b[i][j];
if(b[i][j] && must_with_mn(a[i][j]))
return false;
}
return true;
};
auto yes3 = [&]() {
V<vi> b(h, vi(w));
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
if(must_with_mn(a[i][j]))
b[i][j] = 1;
for(int i = 0; i < h; i++)
for(int j = w - 1; j >= 0; j--) {
if(i + 1 < h) b[i + 1][j] |= b[i][j];
if(j - 1 >= 0) b[i][j - 1] |= b[i][j];
if(b[i][j] && must_with_mx(a[i][j]))
return false;
}
return true;
};
auto yes4 = [&]() {
V<vi> b(h, vi(w));
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
if(must_with_mn(a[i][j]))
b[i][j] = 1;
for(int i = h - 1; i >= 0; i--)
for(int j = w - 1; j >= 0; j--) {
if(i - 1 >= 0) b[i - 1][j] |= b[i][j];
if(j - 1 >= 0) b[i][j - 1] |= b[i][j];
if(b[i][j] && must_with_mx(a[i][j]))
return false;
}
return true;
};
return yes1() || yes2() || yes3() || yes4();
};
int lb = 0, rb = mx - mn - 1;
while(lb <= rb) {
int mb = (lb + rb) / 2;
if(ok(mb))
rb = mb - 1;
else
lb = mb + 1;
}
cout << lb << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
5 ms |
1488 KB |
Output is correct |
17 |
Correct |
11 ms |
1712 KB |
Output is correct |
18 |
Correct |
13 ms |
1716 KB |
Output is correct |
19 |
Correct |
13 ms |
1712 KB |
Output is correct |
20 |
Correct |
11 ms |
1560 KB |
Output is correct |
21 |
Correct |
14 ms |
1836 KB |
Output is correct |
22 |
Correct |
19 ms |
1880 KB |
Output is correct |
23 |
Correct |
16 ms |
1832 KB |
Output is correct |
24 |
Correct |
17 ms |
1660 KB |
Output is correct |
25 |
Correct |
22 ms |
1824 KB |
Output is correct |
26 |
Correct |
16 ms |
1832 KB |
Output is correct |
27 |
Correct |
17 ms |
1824 KB |
Output is correct |
28 |
Correct |
16 ms |
1832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
5 ms |
1488 KB |
Output is correct |
17 |
Correct |
11 ms |
1712 KB |
Output is correct |
18 |
Correct |
13 ms |
1716 KB |
Output is correct |
19 |
Correct |
13 ms |
1712 KB |
Output is correct |
20 |
Correct |
11 ms |
1560 KB |
Output is correct |
21 |
Correct |
14 ms |
1836 KB |
Output is correct |
22 |
Correct |
19 ms |
1880 KB |
Output is correct |
23 |
Correct |
16 ms |
1832 KB |
Output is correct |
24 |
Correct |
17 ms |
1660 KB |
Output is correct |
25 |
Correct |
22 ms |
1824 KB |
Output is correct |
26 |
Correct |
16 ms |
1832 KB |
Output is correct |
27 |
Correct |
17 ms |
1824 KB |
Output is correct |
28 |
Correct |
16 ms |
1832 KB |
Output is correct |
29 |
Correct |
1168 ms |
52308 KB |
Output is correct |
30 |
Correct |
1038 ms |
52296 KB |
Output is correct |
31 |
Correct |
1584 ms |
54900 KB |
Output is correct |
32 |
Correct |
976 ms |
54904 KB |
Output is correct |
33 |
Correct |
1091 ms |
47944 KB |
Output is correct |
34 |
Correct |
1254 ms |
55024 KB |
Output is correct |
35 |
Correct |
1788 ms |
70840 KB |
Output is correct |
36 |
Correct |
1429 ms |
63588 KB |
Output is correct |
37 |
Correct |
2001 ms |
70620 KB |
Output is correct |