#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
using ll = long long;
const int N = 200'000 + 10;
struct FenWick {
int n_node = 0;
vector<int> tree = {};
void resize(int n) { n_node = n; tree.resize(n + 10, 0); }
FenWick() {};
FenWick(int n) { this -> resize(n); };
void Update(int idx, int val) {
for( ; idx <= n_node; idx += (idx & - idx)) tree[idx] = max(tree[idx], val);
return;
}
int Query(int idx) {
int res = 0;
for( ; idx > 0; idx -= (idx & - idx)) res = max(res, tree[idx]);
return res;
}
void reset(int idx) {
for ( ; idx <= n_node; idx += (idx & - idx)) tree[idx] = 0;
return;
}
};
int n, x;
int a[N];
int b[4][N];
int prf[N][2];
int res;
void compress() {
vector<int> Pos = {};
for (int i = 1; i <= n; ++ i) {
Pos.push_back(b[0][i]);
Pos.push_back(b[1][i]);
Pos.push_back(b[2][i]);
Pos.push_back(b[3][i]);
}
sort(Pos.begin(), Pos.end());
Pos.resize(unique(Pos.begin(), Pos.end()) - Pos.begin());
for (int i = 1; i <= n; ++ i) {
b[0][i] = lower_bound(Pos.begin(), Pos.end(), b[0][i]) - Pos.begin() + 1;
b[1][i] = lower_bound(Pos.begin(), Pos.end(), b[1][i]) - Pos.begin() + 1;
b[2][i] = lower_bound(Pos.begin(), Pos.end(), b[2][i]) - Pos.begin() + 1;
b[3][i] = lower_bound(Pos.begin(), Pos.end(), b[3][i]) - Pos.begin() + 1;
}
}
void solve() {
for (int i = 1; i <= n; ++ i) {
b[0][i] = a[i];
b[1][i] = a[i] - x;
b[2][i] = -b[0][i];
b[3][i] = -b[1][i];
}
compress();
FenWick bit[2];
for (int i = 0; i < 2; ++ i) {
bit[i] = FenWick(4 * n);
}
for (int i = 1; i <= n; ++ i) {
int val[2] = {b[0][i], b[1][i]};
prf[i][0] = bit[0].Query(val[0]);
bit[0].Update(val[0] + 1, prf[i][0] + 1);
prf[i][0] = max(prf[i][0], bit[1].Query(val[0]));
prf[i][1] = bit[1].Query(val[1]);
bit[1].Update(val[1] + 1, prf[i][1] + 1);
}
for (int i = 1; i <= n; ++ i) bit[0].reset(b[0][i] + 1);
for (int i = n; i >= 1; -- i) {
int best[2] = {bit[0].Query(b[2][i]), bit[0].Query(b[3][i])};
res = max(res, best[0] + prf[i][0] + 1);
res = max(res, best[1] + prf[i][1] + 1);
bit[0].Update(b[2][i] + 1, best[0] + 1);
}
}
int32_t main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
cin >> n >> x;
for (int i = 1; i <= n; ++ i) cin >> a[i];
solve();
reverse(a + 1, a + 1 + n);
for (int i = 1; i <= n; ++ i) a[i] = -a[i];
solve();
cout << res;
return (0 ^ 0);
}
/// Code by vuavisao
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
372 ms |
14964 KB |
Output is correct |
2 |
Correct |
367 ms |
14984 KB |
Output is correct |
3 |
Correct |
350 ms |
14964 KB |
Output is correct |
4 |
Correct |
389 ms |
14916 KB |
Output is correct |
5 |
Correct |
155 ms |
14192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
4056 KB |
Output is correct |
2 |
Correct |
81 ms |
4028 KB |
Output is correct |
3 |
Correct |
79 ms |
4124 KB |
Output is correct |
4 |
Correct |
47 ms |
3772 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
43 ms |
3780 KB |
Output is correct |
7 |
Correct |
61 ms |
4384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
199 ms |
7720 KB |
Output is correct |
2 |
Correct |
200 ms |
7580 KB |
Output is correct |
3 |
Correct |
437 ms |
15072 KB |
Output is correct |
4 |
Correct |
181 ms |
14196 KB |
Output is correct |
5 |
Correct |
107 ms |
7184 KB |
Output is correct |
6 |
Correct |
187 ms |
13716 KB |
Output is correct |
7 |
Correct |
193 ms |
14348 KB |
Output is correct |
8 |
Correct |
147 ms |
7900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
464 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
372 ms |
14964 KB |
Output is correct |
28 |
Correct |
367 ms |
14984 KB |
Output is correct |
29 |
Correct |
350 ms |
14964 KB |
Output is correct |
30 |
Correct |
389 ms |
14916 KB |
Output is correct |
31 |
Correct |
155 ms |
14192 KB |
Output is correct |
32 |
Correct |
83 ms |
4056 KB |
Output is correct |
33 |
Correct |
81 ms |
4028 KB |
Output is correct |
34 |
Correct |
79 ms |
4124 KB |
Output is correct |
35 |
Correct |
47 ms |
3772 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
43 ms |
3780 KB |
Output is correct |
38 |
Correct |
61 ms |
4384 KB |
Output is correct |
39 |
Correct |
199 ms |
7720 KB |
Output is correct |
40 |
Correct |
200 ms |
7580 KB |
Output is correct |
41 |
Correct |
437 ms |
15072 KB |
Output is correct |
42 |
Correct |
181 ms |
14196 KB |
Output is correct |
43 |
Correct |
107 ms |
7184 KB |
Output is correct |
44 |
Correct |
187 ms |
13716 KB |
Output is correct |
45 |
Correct |
193 ms |
14348 KB |
Output is correct |
46 |
Correct |
147 ms |
7900 KB |
Output is correct |
47 |
Correct |
180 ms |
7596 KB |
Output is correct |
48 |
Correct |
193 ms |
7596 KB |
Output is correct |
49 |
Correct |
401 ms |
14916 KB |
Output is correct |
50 |
Correct |
163 ms |
14212 KB |
Output is correct |
51 |
Correct |
155 ms |
11836 KB |
Output is correct |
52 |
Correct |
213 ms |
14208 KB |
Output is correct |
53 |
Correct |
193 ms |
14240 KB |
Output is correct |
54 |
Correct |
204 ms |
14960 KB |
Output is correct |
55 |
Correct |
292 ms |
15216 KB |
Output is correct |