#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e3 + 2;
int h, w, a[maxN][maxN], mxA_i;
bool b[maxN][maxN];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> h >> w;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> a[i][j], mxA_i = max(mxA_i, a[i][j]);
auto small_check = [&](int k) {
int max_right = w;
int mn = int(1e9), mx = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < max_right; j++) if (b[i][j])
max_right = j;
for (int j = max_right; j < w; j++)
mn = min(mn, a[i][j]), mx = max(mx, a[i][j]);
}
return mx - mn <= k;
};
auto reverse_rows = [&]() {
for (int i = 0; i < h; i++)
for (int j = 0; j < w / 2; j++)
swap(a[i][j], a[i][w - j - 1]), swap(b[i][j], b[i][w - j - 1]);
};
auto reverse_cols = [&]() {
for (int i = 0; i < h / 2; i++)
for (int j = 0; j < w; j++)
swap(a[i][j], a[h - i - 1][j]), swap(b[i][j], b[h - i - 1][j]);
};
auto bin_func = [&](int k) {
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
b[i][j] = (mxA_i - a[i][j] > k);
bool res = false;
reverse_rows(); res |= small_check(k);
reverse_cols(); res |= small_check(k);
reverse_rows(); res |= small_check(k);
reverse_cols(); res |= small_check(k);
return res;
};
int lo = 1, hi = int(1e9), ans = hi;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (bin_func(mid)) ans = mid, hi = mid - 1;
else lo = mid + 1;
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
1620 KB |
Output is correct |
17 |
Correct |
11 ms |
1684 KB |
Output is correct |
18 |
Correct |
11 ms |
1688 KB |
Output is correct |
19 |
Correct |
16 ms |
1620 KB |
Output is correct |
20 |
Correct |
10 ms |
1492 KB |
Output is correct |
21 |
Correct |
17 ms |
1692 KB |
Output is correct |
22 |
Correct |
14 ms |
1684 KB |
Output is correct |
23 |
Correct |
13 ms |
1620 KB |
Output is correct |
24 |
Correct |
11 ms |
1492 KB |
Output is correct |
25 |
Correct |
18 ms |
1620 KB |
Output is correct |
26 |
Correct |
14 ms |
1620 KB |
Output is correct |
27 |
Correct |
12 ms |
1620 KB |
Output is correct |
28 |
Correct |
12 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
8 ms |
1620 KB |
Output is correct |
17 |
Correct |
11 ms |
1684 KB |
Output is correct |
18 |
Correct |
11 ms |
1688 KB |
Output is correct |
19 |
Correct |
16 ms |
1620 KB |
Output is correct |
20 |
Correct |
10 ms |
1492 KB |
Output is correct |
21 |
Correct |
17 ms |
1692 KB |
Output is correct |
22 |
Correct |
14 ms |
1684 KB |
Output is correct |
23 |
Correct |
13 ms |
1620 KB |
Output is correct |
24 |
Correct |
11 ms |
1492 KB |
Output is correct |
25 |
Correct |
18 ms |
1620 KB |
Output is correct |
26 |
Correct |
14 ms |
1620 KB |
Output is correct |
27 |
Correct |
12 ms |
1620 KB |
Output is correct |
28 |
Correct |
12 ms |
1620 KB |
Output is correct |
29 |
Correct |
1066 ms |
18928 KB |
Output is correct |
30 |
Correct |
1090 ms |
19900 KB |
Output is correct |
31 |
Correct |
1193 ms |
19928 KB |
Output is correct |
32 |
Correct |
1178 ms |
19988 KB |
Output is correct |
33 |
Correct |
1006 ms |
17512 KB |
Output is correct |
34 |
Correct |
1123 ms |
19900 KB |
Output is correct |
35 |
Correct |
1337 ms |
19908 KB |
Output is correct |
36 |
Correct |
1176 ms |
19784 KB |
Output is correct |
37 |
Correct |
1342 ms |
19912 KB |
Output is correct |