#include <bits/stdc++.h>
using namespace std;
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(0);
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using ld = long double;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const ll LINF = ll(4e18) + ll(2e15);
const double EPS = 1e-9;
static int LamyIsCute = []() {
EmiliaMyWife
return 48763;
}();
signed main() {
int n, m;
cin >> n >> m;
vector<vector<int>> arr(n + 2, vector<int>(m + 1));
int max_val = -INF, min_val = INF;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
cin >> arr[i][j];
max_val = max(max_val, arr[i][j]);
min_val = min(min_val, arr[i][j]);
}
auto rot = [&]() {
reverse(arr.begin(), arr.end());
};
auto go = [&](int k) {
pair<int, int> cur_interval = {0, 0};
for(int i = 1; i <= n; i++) {
vector<int> suf_min(m + 1);
for(int j = m; j; j--) {
suf_min[j] = arr[i][j];
if(j + 1 <= m)
suf_min[j] = min(suf_min[j], suf_min[j + 1]);
}
pair<int, int> new_interval = {INF, -INF};
for(int j = 0, mx = 0; j <= m; j++) {
if(j)
mx = max(mx, arr[i][j]);
int r = (j < m ? suf_min[j + 1] : INF);
if(mx <= min_val + k && max_val - k <= r && cur_interval.first <= j) {
new_interval.first = min(new_interval.first, j);
new_interval.second = max(new_interval.second, j);
}
}
cur_interval = new_interval;
}
if(cur_interval.first <= cur_interval.second)
return true;
cur_interval = {0, 0};
for(int i = 1; i <= n; i++) {
vector<int> suf_max(m + 1);
for(int j = m; j; j--) {
suf_max[j] = arr[i][j];
if(j + 1 <= m)
suf_max[j] = max(suf_max[j], suf_max[j + 1]);
}
pair<int, int> new_interval = {INF, -INF};
for(int j = 0, mn = INF; j <= m; j++) {
if(j)
mn = min(mn, arr[i][j]);
int r = (j < m ? suf_max[j + 1] : 0);
if(r <= min_val + k && max_val - k <= mn && cur_interval.first <= j) {
new_interval.first = min(new_interval.first, j);
new_interval.second = max(new_interval.second, j);
}
}
cur_interval = new_interval;
}
if(cur_interval.first <= cur_interval.second)
return true;
return false;
};
auto check = [&](int k) {
bool ok = false;
for(int i = 0; i < 2; i++)
ok |= go(k), rot();
return ok;
};
int l = 0, r = INF;
while(l < r) {
int m = l + r >> 1;
if(check(m))
r = m;
else
l = m + 1;
}
cout << l << '\n';
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:92:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
92 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
15 ms |
460 KB |
Output is correct |
17 |
Correct |
22 ms |
716 KB |
Output is correct |
18 |
Correct |
22 ms |
716 KB |
Output is correct |
19 |
Correct |
24 ms |
708 KB |
Output is correct |
20 |
Correct |
22 ms |
588 KB |
Output is correct |
21 |
Correct |
30 ms |
844 KB |
Output is correct |
22 |
Correct |
29 ms |
820 KB |
Output is correct |
23 |
Correct |
34 ms |
844 KB |
Output is correct |
24 |
Correct |
24 ms |
716 KB |
Output is correct |
25 |
Correct |
29 ms |
940 KB |
Output is correct |
26 |
Correct |
28 ms |
836 KB |
Output is correct |
27 |
Correct |
28 ms |
844 KB |
Output is correct |
28 |
Correct |
26 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
15 ms |
460 KB |
Output is correct |
17 |
Correct |
22 ms |
716 KB |
Output is correct |
18 |
Correct |
22 ms |
716 KB |
Output is correct |
19 |
Correct |
24 ms |
708 KB |
Output is correct |
20 |
Correct |
22 ms |
588 KB |
Output is correct |
21 |
Correct |
30 ms |
844 KB |
Output is correct |
22 |
Correct |
29 ms |
820 KB |
Output is correct |
23 |
Correct |
34 ms |
844 KB |
Output is correct |
24 |
Correct |
24 ms |
716 KB |
Output is correct |
25 |
Correct |
29 ms |
940 KB |
Output is correct |
26 |
Correct |
28 ms |
836 KB |
Output is correct |
27 |
Correct |
28 ms |
844 KB |
Output is correct |
28 |
Correct |
26 ms |
844 KB |
Output is correct |
29 |
Correct |
2021 ms |
37392 KB |
Output is correct |
30 |
Correct |
2097 ms |
36396 KB |
Output is correct |
31 |
Correct |
1999 ms |
39056 KB |
Output is correct |
32 |
Correct |
2110 ms |
39100 KB |
Output is correct |
33 |
Correct |
1758 ms |
34120 KB |
Output is correct |
34 |
Correct |
2221 ms |
39328 KB |
Output is correct |
35 |
Correct |
2588 ms |
54820 KB |
Output is correct |
36 |
Correct |
2384 ms |
47700 KB |
Output is correct |
37 |
Correct |
2810 ms |
51968 KB |
Output is correct |