#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 2000 + 5;
int grid[MAX_N][MAX_N];
bool used[MAX_N][MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
}
}
auto check = [&](int x) {
bool ok = false;
// // 1)
// {
// int first_min = 1e9;
// int first_max = 0;
// int last_used = m - 1;
// for (int i = 0; i < n; ++i) {
// int prev_used = last_used;
// for (int j = 0; j <= prev_used; ++j) {
// int curr_min = min(first_min, grid[i][j]);
// int curr_max = max(first_max, grid[i][j]);
// if (curr_max - curr_min > x) {
// last_used = j - 1;
// break;
// } else {
// first_min = curr_min;
// first_max = curr_max;
// last_used = j;
// used[i][j] = true;
// }
// }
// }
// int second_min = 1e9;
// int second_max = 0;
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < m; ++j) {
// if (!used[i][j]) {
// second_min = min(second_min, grid[i][j]);
// second_max = max(second_max, grid[i][j]);
// } else {
// used[i][j] = false;
// }
// }
// }
// ok |= second_max - second_min <= x;
// }
// 2)
{
int first_min = 1e9;
int first_max = 0;
int last_used = 0;
for (int i = 0; i < n; ++i) {
int prev_used = last_used;
for (int j = m - 1; j >= prev_used; --j) {
int curr_min = min(first_min, grid[i][j]);
int curr_max = max(first_max, grid[i][j]);
if (curr_max - curr_min > x) {
last_used = j + 1;
break;
} else {
first_min = curr_min;
first_max = curr_max;
last_used = j;
used[i][j] = true;
}
}
}
int second_min = 1e9;
int second_max = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!used[i][j]) {
second_min = min(second_min, grid[i][j]);
second_max = max(second_max, grid[i][j]);
} else {
used[i][j] = false;
}
}
}
ok |= second_max - second_min <= x;
}
// // 3)
// {
// int first_min = 1e9;
// int first_max = 0;
// int last_used = m - 1;
// for (int i = n - 1; i >= 0; --i) {
// int prev_used = last_used;
// for (int j = 0; j <= prev_used; ++j) {
// int curr_min = min(first_min, grid[i][j]);
// int curr_max = max(first_max, grid[i][j]);
// if (curr_max - curr_min > x) {
// last_used = j - 1;
// break;
// } else {
// first_min = curr_min;
// first_max = curr_max;
// last_used = j;
// used[i][j] = true;
// }
// }
// }
// int second_min = 1e9;
// int second_max = 0;
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < m; ++j) {
// if (!used[i][j]) {
// second_min = min(second_min, grid[i][j]);
// second_max = max(second_max, grid[i][j]);
// } else {
// used[i][j] = false;
// }
// }
// }
// ok |= second_max - second_min <= x;
// }
// 4)
{
int first_min = 1e9;
int first_max = 0;
int last_used = 0;
for (int i = n - 1; i >= 0; --i) {
int prev_used = last_used;
for (int j = m - 1; j >= prev_used; --j) {
int curr_min = min(first_min, grid[i][j]);
int curr_max = max(first_max, grid[i][j]);
if (curr_max - curr_min > x) {
last_used = j + 1;
break;
} else {
first_min = curr_min;
first_max = curr_max;
last_used = j;
used[i][j] = true;
}
}
}
int second_min = 1e9;
int second_max = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!used[i][j]) {
second_min = min(second_min, grid[i][j]);
second_max = max(second_max, grid[i][j]);
} else {
used[i][j] = false;
}
}
}
ok |= second_max - second_min <= x;
}
// // 5)
// {
// int first_min = 1e9;
// int first_max = 0;
// int last_used = n - 1;
// for (int j = 0; j < m; ++j) {
// int prev_used = last_used;
// for (int i = 0; i <= prev_used; ++i) {
// int curr_min = min(first_min, grid[i][j]);
// int curr_max = max(first_max, grid[i][j]);
// if (curr_max - curr_min > x) {
// last_used = i - 1;
// break;
// } else {
// first_min = curr_min;
// first_max = curr_max;
// last_used = i;
// used[i][j] = true;
// }
// }
// }
// int second_min = 1e9;
// int second_max = 0;
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < m; ++j) {
// if (!used[i][j]) {
// second_min = min(second_min, grid[i][j]);
// second_max = max(second_max, grid[i][j]);
// } else {
// used[i][j] = false;
// }
// }
// }
// ok |= second_max - second_min <= x;
// }
// 6)
{
int first_min = 1e9;
int first_max = 0;
int last_used = n - 1;
for (int j = m - 1; j >= 0; --j) {
int prev_used = last_used;
for (int i = 0; i <= prev_used; ++i) {
int curr_min = min(first_min, grid[i][j]);
int curr_max = max(first_max, grid[i][j]);
if (curr_max - curr_min > x) {
last_used = i - 1;
break;
} else {
first_min = curr_min;
first_max = curr_max;
last_used = i;
used[i][j] = true;
}
}
}
int second_min = 1e9;
int second_max = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!used[i][j]) {
second_min = min(second_min, grid[i][j]);
second_max = max(second_max, grid[i][j]);
} else {
used[i][j] = false;
}
}
}
ok |= second_max - second_min <= x;
}
// // 7)
// {
// int first_min = 1e9;
// int first_max = 0;
// int last_used = 0;
// for (int j = 0; j < m; ++j) {
// int prev_used = last_used;
// for (int i = n - 1; i >= prev_used; --i) {
// int curr_min = min(first_min, grid[i][j]);
// int curr_max = max(first_max, grid[i][j]);
// if (curr_max - curr_min > x) {
// last_used = i + 1;
// break;
// } else {
// first_min = curr_min;
// first_max = curr_max;
// last_used = i;
// used[i][j] = true;
// }
// }
// }
// int second_min = 1e9;
// int second_max = 0;
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < m; ++j) {
// if (!used[i][j]) {
// second_min = min(second_min, grid[i][j]);
// second_max = max(second_max, grid[i][j]);
// } else {
// used[i][j] = false;
// }
// }
// }
// ok |= second_max - second_min <= x;
// }
// 8)
{
int first_min = 1e9;
int first_max = 0;
int last_used = 0;
for (int j = m - 1; j >= 0; --j) {
int prev_used = last_used;
for (int i = n - 1; i >= prev_used; --i) {
int curr_min = min(first_min, grid[i][j]);
int curr_max = max(first_max, grid[i][j]);
if (curr_max - curr_min > x) {
last_used = i + 1;
break;
} else {
first_min = curr_min;
first_max = curr_max;
last_used = i;
used[i][j] = true;
}
}
}
int second_min = 1e9;
int second_max = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!used[i][j]) {
second_min = min(second_min, grid[i][j]);
second_max = max(second_max, grid[i][j]);
} else {
used[i][j] = false;
}
}
}
ok |= second_max - second_min <= x;
}
return ok;
};
int l = -1;
int r = 1e9 + 1;
while (l != r - 1) {
int mid = (l + r) / 2;
if (check(mid)) {
r = mid;
} else {
l = mid;
}
}
cout << r << '\n';
return 0;
}
/*
1 5
1 2 1 2 1
*/
# |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
11 ms |
1644 KB |
Output is correct |
17 |
Correct |
13 ms |
1876 KB |
Output is correct |
18 |
Correct |
12 ms |
1884 KB |
Output is correct |
19 |
Correct |
12 ms |
1876 KB |
Output is correct |
20 |
Correct |
14 ms |
1768 KB |
Output is correct |
21 |
Correct |
17 ms |
2000 KB |
Output is correct |
22 |
Correct |
12 ms |
2004 KB |
Output is correct |
23 |
Correct |
13 ms |
2004 KB |
Output is correct |
24 |
Correct |
10 ms |
1840 KB |
Output is correct |
25 |
Correct |
12 ms |
1984 KB |
Output is correct |
26 |
Correct |
15 ms |
2004 KB |
Output is correct |
27 |
Correct |
14 ms |
1992 KB |
Output is correct |
28 |
Correct |
13 ms |
1992 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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
11 ms |
1644 KB |
Output is correct |
17 |
Correct |
13 ms |
1876 KB |
Output is correct |
18 |
Correct |
12 ms |
1884 KB |
Output is correct |
19 |
Correct |
12 ms |
1876 KB |
Output is correct |
20 |
Correct |
14 ms |
1768 KB |
Output is correct |
21 |
Correct |
17 ms |
2000 KB |
Output is correct |
22 |
Correct |
12 ms |
2004 KB |
Output is correct |
23 |
Correct |
13 ms |
2004 KB |
Output is correct |
24 |
Correct |
10 ms |
1840 KB |
Output is correct |
25 |
Correct |
12 ms |
1984 KB |
Output is correct |
26 |
Correct |
15 ms |
2004 KB |
Output is correct |
27 |
Correct |
14 ms |
1992 KB |
Output is correct |
28 |
Correct |
13 ms |
1992 KB |
Output is correct |
29 |
Correct |
2240 ms |
41044 KB |
Output is correct |
30 |
Correct |
2183 ms |
41580 KB |
Output is correct |
31 |
Correct |
2342 ms |
43060 KB |
Output is correct |
32 |
Correct |
2465 ms |
43088 KB |
Output is correct |
33 |
Correct |
1925 ms |
37672 KB |
Output is correct |
34 |
Correct |
2398 ms |
43140 KB |
Output is correct |
35 |
Correct |
1369 ms |
58692 KB |
Output is correct |
36 |
Correct |
1168 ms |
53524 KB |
Output is correct |
37 |
Correct |
1448 ms |
58796 KB |
Output is correct |