#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int MXN = 2005;
int n, m, a[MXN][MXN], b[MXN][MXN];
bitset<MXN> lu[MXN];
pii C[MXN];
int big = 0, small = LLONG_MAX;
int l, r;
bool LU1() {
for (int i = 0; i < n; i++) {
lu[i].reset();
for (int j = 0; j < m; j++) {
// if (i) lu[i][j] |= lu[i - 1][j];
// if (j) lu[i][j] |= lu[i][j - 1];
if (i && lu[i - 1][j]) lu[i][j] = true;
if (j && lu[i][j - 1]) lu[i][j] = true;
if (b[i][j] == 2) lu[i][j] = true;
if (b[i][j] == 1 && lu[i][j]) return false;
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) cout << (lu[i][j] ? '1' : '0');
// cout << endl;
// }
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// if (b[i][j] == 1 && lu[i][j]) return false;
// }
// }
return true;
}
bool LU2() {
for (int i = 0; i < n; i++) {
lu[i].reset();
for (int j = 0; j < m; j++) {
// if (i) lu[i][j] |= lu[i - 1][j];
// if (j) lu[i][j] |= lu[i][j - 1];
if (i && lu[i - 1][j]) lu[i][j] = true;
if (j && lu[i][j - 1]) lu[i][j] = true;
if (b[i][j] == 1) lu[i][j] = true;
if (b[i][j] == 2 && lu[i][j]) return false;
}
}
return true;
}
bool CHECK() {
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// cout << b[i][j] << ' ';
// }
// cout << endl;
// }
if (LU1()) return true;
if (LU2()) return true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m / 2; j++) {
swap(b[i][j], b[i][m - j - 1]);
}
}
if (LU1()) return true;
if (LU2()) return true;
return false;
}
bool check(int x) {
if (big - 2 * x < 1) { // cross
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] < big - x) b[i][j] = 1;
else if (x < a[i][j]) b[i][j] = 2;
else b[i][j] = 0;
}
}
} else { // not cross
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] <= x) b[i][j] = 1;
else if (big - x <= a[i][j]) b[i][j] = 2;
else return false;
}
}
}
// cout << x << endl;
bool f = CHECK();
// cout << (f ? "YES" : "NO") << endl;
return f;
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
cin >> a[i][j];
big = max(big, a[i][j]);
small = min(small, a[i][j]);
}
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
a[i][j] -= small;
}
big -= small;
int l = -1, r = big;
while (l + 1 < r) {
int mid = (l + r) >> 1;
(check(mid) ? r : l) = mid;
}
cout << r << endl;
// for (int i = 0; i < 4; i++) {
// for (int j = 0; j < 4; j++) {
// cin >> b[i][j];
// }
// }
// n = 4, m = 4;
// cout << LU1() << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
4 ms |
2604 KB |
Output is correct |
17 |
Correct |
8 ms |
2900 KB |
Output is correct |
18 |
Correct |
6 ms |
2772 KB |
Output is correct |
19 |
Correct |
11 ms |
2900 KB |
Output is correct |
20 |
Correct |
9 ms |
2604 KB |
Output is correct |
21 |
Correct |
12 ms |
2940 KB |
Output is correct |
22 |
Correct |
8 ms |
2900 KB |
Output is correct |
23 |
Correct |
14 ms |
2904 KB |
Output is correct |
24 |
Correct |
7 ms |
2652 KB |
Output is correct |
25 |
Correct |
16 ms |
2924 KB |
Output is correct |
26 |
Correct |
11 ms |
3012 KB |
Output is correct |
27 |
Correct |
14 ms |
2900 KB |
Output is correct |
28 |
Correct |
12 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
4 ms |
2604 KB |
Output is correct |
17 |
Correct |
8 ms |
2900 KB |
Output is correct |
18 |
Correct |
6 ms |
2772 KB |
Output is correct |
19 |
Correct |
11 ms |
2900 KB |
Output is correct |
20 |
Correct |
9 ms |
2604 KB |
Output is correct |
21 |
Correct |
12 ms |
2940 KB |
Output is correct |
22 |
Correct |
8 ms |
2900 KB |
Output is correct |
23 |
Correct |
14 ms |
2904 KB |
Output is correct |
24 |
Correct |
7 ms |
2652 KB |
Output is correct |
25 |
Correct |
16 ms |
2924 KB |
Output is correct |
26 |
Correct |
11 ms |
3012 KB |
Output is correct |
27 |
Correct |
14 ms |
2900 KB |
Output is correct |
28 |
Correct |
12 ms |
2904 KB |
Output is correct |
29 |
Correct |
601 ms |
82552 KB |
Output is correct |
30 |
Correct |
664 ms |
85144 KB |
Output is correct |
31 |
Correct |
909 ms |
86756 KB |
Output is correct |
32 |
Correct |
569 ms |
86696 KB |
Output is correct |
33 |
Correct |
382 ms |
76000 KB |
Output is correct |
34 |
Correct |
730 ms |
86844 KB |
Output is correct |
35 |
Correct |
967 ms |
102420 KB |
Output is correct |
36 |
Correct |
697 ms |
97000 KB |
Output is correct |
37 |
Correct |
1306 ms |
102572 KB |
Output is correct |