#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, m, mx, grid[2000][2000], tmp[2000][2000];
void rot() {
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++)
tmp[j][n - i - 1] = grid[i][j];
swap(n, m);
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++)
grid[i][j] = tmp[i][j];
}
bool check(int diff) {
for (int _:{0,0,0,0}) {
int lmx = 0, lmn = mx, lst = m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < lst; j++) if (grid[i][j] < mx - diff) {
lst = j;
break;
}
for (int j = lst; j < m; j++) lmx = max(lmx, grid[i][j]), lmn = min(lmn, grid[i][j]);
}
if (lmx - lmn <= diff) return true;
rot();
}
return false;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
cin >> grid[i][j];
mx = max(mx, grid[i][j]);
}
int l = 0, r = mx;
while (l != r) {
int mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l;
return 0;
}
Compilation message
joioi.cpp: In function 'bool check(int)':
joioi.cpp:16:11: warning: unused variable '_' [-Wunused-variable]
16 | for (int _:{0,0,0,0}) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
2028 KB |
Output is correct |
16 |
Correct |
5 ms |
2284 KB |
Output is correct |
17 |
Correct |
12 ms |
2540 KB |
Output is correct |
18 |
Correct |
12 ms |
2540 KB |
Output is correct |
19 |
Correct |
10 ms |
2540 KB |
Output is correct |
20 |
Correct |
9 ms |
2540 KB |
Output is correct |
21 |
Correct |
20 ms |
2668 KB |
Output is correct |
22 |
Correct |
14 ms |
2668 KB |
Output is correct |
23 |
Correct |
14 ms |
2668 KB |
Output is correct |
24 |
Correct |
19 ms |
2540 KB |
Output is correct |
25 |
Correct |
19 ms |
2668 KB |
Output is correct |
26 |
Correct |
19 ms |
2668 KB |
Output is correct |
27 |
Correct |
16 ms |
2668 KB |
Output is correct |
28 |
Correct |
24 ms |
2668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
2028 KB |
Output is correct |
16 |
Correct |
5 ms |
2284 KB |
Output is correct |
17 |
Correct |
12 ms |
2540 KB |
Output is correct |
18 |
Correct |
12 ms |
2540 KB |
Output is correct |
19 |
Correct |
10 ms |
2540 KB |
Output is correct |
20 |
Correct |
9 ms |
2540 KB |
Output is correct |
21 |
Correct |
20 ms |
2668 KB |
Output is correct |
22 |
Correct |
14 ms |
2668 KB |
Output is correct |
23 |
Correct |
14 ms |
2668 KB |
Output is correct |
24 |
Correct |
19 ms |
2540 KB |
Output is correct |
25 |
Correct |
19 ms |
2668 KB |
Output is correct |
26 |
Correct |
19 ms |
2668 KB |
Output is correct |
27 |
Correct |
16 ms |
2668 KB |
Output is correct |
28 |
Correct |
24 ms |
2668 KB |
Output is correct |
29 |
Correct |
2281 ms |
32748 KB |
Output is correct |
30 |
Correct |
2089 ms |
31724 KB |
Output is correct |
31 |
Correct |
2222 ms |
32876 KB |
Output is correct |
32 |
Correct |
2627 ms |
32876 KB |
Output is correct |
33 |
Correct |
2209 ms |
31596 KB |
Output is correct |
34 |
Correct |
2035 ms |
32416 KB |
Output is correct |
35 |
Correct |
3952 ms |
32360 KB |
Output is correct |
36 |
Correct |
2804 ms |
32108 KB |
Output is correct |
37 |
Correct |
2979 ms |
32364 KB |
Output is correct |