#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int mx = 0;
vector<vector<int>> a(n, vector<int> (m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
}
}
auto check = [&](int mid) {
int la = m - 1;
int min_val = mx - mid;
vector<vector<bool>> region(n, vector<bool> (m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= la; ++j) {//coli <= coli-1
if (a[i][j] < min_val) {
la = j - 1;
break;
}
region[i][j] = 1;
}
}
vector<int> bmx(2), bmn(2, 1e9);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
bmx[region[i][j]] = max(bmx[region[i][j]], a[i][j]);
bmn[region[i][j]] = min(bmn[region[i][j]], a[i][j]);
}
}
return max(bmx[0] - bmn[0], bmx[1] - bmn[1]) <= mid;
};
auto solve = [&]() {
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};
auto flip_rows = [&]() {
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < m; ++j) {
swap(a[i][j], a[n - i - 1][j]);
}
}
};
auto flip_cols = [&]() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m / 2; ++j) {
swap(a[i][j], a[i][m - j - 1]);
}
}
};
int ans = solve(); //0, 0
flip_cols();
ans = min(ans, solve()); //0, 1
flip_rows();
ans = min(ans, solve()); //1, 1
flip_cols();
ans = min(ans, solve());//1, 0
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
25 ms |
604 KB |
Output is correct |
17 |
Correct |
25 ms |
860 KB |
Output is correct |
18 |
Correct |
25 ms |
856 KB |
Output is correct |
19 |
Correct |
25 ms |
1112 KB |
Output is correct |
20 |
Correct |
22 ms |
600 KB |
Output is correct |
21 |
Correct |
23 ms |
856 KB |
Output is correct |
22 |
Correct |
23 ms |
860 KB |
Output is correct |
23 |
Correct |
22 ms |
860 KB |
Output is correct |
24 |
Correct |
20 ms |
940 KB |
Output is correct |
25 |
Correct |
23 ms |
860 KB |
Output is correct |
26 |
Correct |
23 ms |
860 KB |
Output is correct |
27 |
Correct |
23 ms |
860 KB |
Output is correct |
28 |
Correct |
23 ms |
1028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
600 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
25 ms |
604 KB |
Output is correct |
17 |
Correct |
25 ms |
860 KB |
Output is correct |
18 |
Correct |
25 ms |
856 KB |
Output is correct |
19 |
Correct |
25 ms |
1112 KB |
Output is correct |
20 |
Correct |
22 ms |
600 KB |
Output is correct |
21 |
Correct |
23 ms |
856 KB |
Output is correct |
22 |
Correct |
23 ms |
860 KB |
Output is correct |
23 |
Correct |
22 ms |
860 KB |
Output is correct |
24 |
Correct |
20 ms |
940 KB |
Output is correct |
25 |
Correct |
23 ms |
860 KB |
Output is correct |
26 |
Correct |
23 ms |
860 KB |
Output is correct |
27 |
Correct |
23 ms |
860 KB |
Output is correct |
28 |
Correct |
23 ms |
1028 KB |
Output is correct |
29 |
Correct |
2192 ms |
38412 KB |
Output is correct |
30 |
Correct |
2136 ms |
37336 KB |
Output is correct |
31 |
Correct |
2321 ms |
40428 KB |
Output is correct |
32 |
Correct |
2323 ms |
40212 KB |
Output is correct |
33 |
Correct |
1988 ms |
34756 KB |
Output is correct |
34 |
Correct |
2285 ms |
40264 KB |
Output is correct |
35 |
Correct |
2094 ms |
55812 KB |
Output is correct |
36 |
Correct |
1776 ms |
48592 KB |
Output is correct |
37 |
Correct |
2076 ms |
56096 KB |
Output is correct |