#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2001;
int a[N][N], n, m, mx, px, py;
bool t[N][N];
void upd() {
mx = -1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (mx < a[i][j]) {
mx = a[i][j];
px = i, py = j;
}
}
}
}
void mirror() {
for (int i = 0; i < n; ++i) {
reverse(a[i], a[i] + m);
}
upd();
}
void rotate180() {
for (int j = 0; j < m; ++j) {
for (int i = 0; i < n / 2; ++i) {
swap(a[i][j], a[n - i - 1][j]);
}
}
upd();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
upd();
auto check = [&](int mid) -> bool {
for (int _ = 0; _ < 2; ++_) {
for (int iter = 0; iter < 2; ++iter) {
memset(t, 0, sizeof(t));
int pos = m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < pos; ++j) {
if (mx - a[i][j] <= mid) {
t[i][j] = true;
} else {
pos = j;
break;
}
}
}
int Max = -1, Min = mx;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!t[i][j]) {
Max = max(a[i][j], Max);
Min = min(a[i][j], Min);
}
}
}
if (Max == -1 || Max - Min <= mid) {
return true;
}
rotate180();
}
mirror();
}
return false;
};
int l = -1, r = mx;
while (l + 1 < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid;
}
}
cout << r;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
4 ms |
4164 KB |
Output is correct |
3 |
Correct |
11 ms |
4284 KB |
Output is correct |
4 |
Correct |
9 ms |
4280 KB |
Output is correct |
5 |
Correct |
8 ms |
4280 KB |
Output is correct |
6 |
Correct |
11 ms |
4180 KB |
Output is correct |
7 |
Correct |
12 ms |
4276 KB |
Output is correct |
8 |
Correct |
9 ms |
4180 KB |
Output is correct |
9 |
Correct |
10 ms |
4180 KB |
Output is correct |
10 |
Correct |
10 ms |
4276 KB |
Output is correct |
11 |
Correct |
9 ms |
4180 KB |
Output is correct |
12 |
Correct |
11 ms |
4172 KB |
Output is correct |
13 |
Correct |
10 ms |
4280 KB |
Output is correct |
14 |
Correct |
10 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
4 ms |
4164 KB |
Output is correct |
3 |
Correct |
11 ms |
4284 KB |
Output is correct |
4 |
Correct |
9 ms |
4280 KB |
Output is correct |
5 |
Correct |
8 ms |
4280 KB |
Output is correct |
6 |
Correct |
11 ms |
4180 KB |
Output is correct |
7 |
Correct |
12 ms |
4276 KB |
Output is correct |
8 |
Correct |
9 ms |
4180 KB |
Output is correct |
9 |
Correct |
10 ms |
4180 KB |
Output is correct |
10 |
Correct |
10 ms |
4276 KB |
Output is correct |
11 |
Correct |
9 ms |
4180 KB |
Output is correct |
12 |
Correct |
11 ms |
4172 KB |
Output is correct |
13 |
Correct |
10 ms |
4280 KB |
Output is correct |
14 |
Correct |
10 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4308 KB |
Output is correct |
16 |
Correct |
6 ms |
5248 KB |
Output is correct |
17 |
Correct |
21 ms |
5460 KB |
Output is correct |
18 |
Correct |
20 ms |
5472 KB |
Output is correct |
19 |
Correct |
14 ms |
5476 KB |
Output is correct |
20 |
Correct |
22 ms |
5332 KB |
Output is correct |
21 |
Correct |
39 ms |
5588 KB |
Output is correct |
22 |
Correct |
33 ms |
5460 KB |
Output is correct |
23 |
Correct |
27 ms |
5580 KB |
Output is correct |
24 |
Correct |
24 ms |
5332 KB |
Output is correct |
25 |
Correct |
24 ms |
5568 KB |
Output is correct |
26 |
Correct |
22 ms |
5588 KB |
Output is correct |
27 |
Correct |
25 ms |
5588 KB |
Output is correct |
28 |
Correct |
29 ms |
5596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
4 ms |
4164 KB |
Output is correct |
3 |
Correct |
11 ms |
4284 KB |
Output is correct |
4 |
Correct |
9 ms |
4280 KB |
Output is correct |
5 |
Correct |
8 ms |
4280 KB |
Output is correct |
6 |
Correct |
11 ms |
4180 KB |
Output is correct |
7 |
Correct |
12 ms |
4276 KB |
Output is correct |
8 |
Correct |
9 ms |
4180 KB |
Output is correct |
9 |
Correct |
10 ms |
4180 KB |
Output is correct |
10 |
Correct |
10 ms |
4276 KB |
Output is correct |
11 |
Correct |
9 ms |
4180 KB |
Output is correct |
12 |
Correct |
11 ms |
4172 KB |
Output is correct |
13 |
Correct |
10 ms |
4280 KB |
Output is correct |
14 |
Correct |
10 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4308 KB |
Output is correct |
16 |
Correct |
6 ms |
5248 KB |
Output is correct |
17 |
Correct |
21 ms |
5460 KB |
Output is correct |
18 |
Correct |
20 ms |
5472 KB |
Output is correct |
19 |
Correct |
14 ms |
5476 KB |
Output is correct |
20 |
Correct |
22 ms |
5332 KB |
Output is correct |
21 |
Correct |
39 ms |
5588 KB |
Output is correct |
22 |
Correct |
33 ms |
5460 KB |
Output is correct |
23 |
Correct |
27 ms |
5580 KB |
Output is correct |
24 |
Correct |
24 ms |
5332 KB |
Output is correct |
25 |
Correct |
24 ms |
5568 KB |
Output is correct |
26 |
Correct |
22 ms |
5588 KB |
Output is correct |
27 |
Correct |
25 ms |
5588 KB |
Output is correct |
28 |
Correct |
29 ms |
5596 KB |
Output is correct |
29 |
Correct |
1650 ms |
41192 KB |
Output is correct |
30 |
Correct |
1600 ms |
41364 KB |
Output is correct |
31 |
Correct |
1859 ms |
42972 KB |
Output is correct |
32 |
Correct |
2379 ms |
42936 KB |
Output is correct |
33 |
Correct |
1741 ms |
38004 KB |
Output is correct |
34 |
Correct |
1883 ms |
43048 KB |
Output is correct |
35 |
Correct |
3366 ms |
58564 KB |
Output is correct |
36 |
Correct |
1991 ms |
53428 KB |
Output is correct |
37 |
Correct |
2324 ms |
58712 KB |
Output is correct |