#include <bits/stdc++.h>
using namespace std;
const int N = 200'000 + 10;
int n, x;
int a[N];
vector<int> rrh;
int f[N << 2];
void update(int s, int l, int r, int i, int value) {
if (i < l || i > r) return;
if (l == r) {
f[s] = value;
return;
}
int mid = l + r >> 1;
update(s << 1, l, mid, i, value); update(s << 1 | 1, mid + 1, r, i, value);
f[s] = max(f[s << 1], f[s << 1 | 1]);
}
int query(int s, int l, int r, int u, int v) {
if (v < l || u > r) return 0;
if (u <= l && r <= v) return f[s];
int mid = l + r >> 1;
return max(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}
int fwd[N], bwd[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> x;
for (int i = 1; i <= n; ++i) cin >> a[i];
rrh = vector<int> (a + 1, a + n + 1);
sort(rrh.begin(), rrh.end());
rrh.resize(unique(rrh.begin(), rrh.end()) - rrh.begin());
for (int i = 1; i <= n; ++i) {
int r = lower_bound(rrh.begin(), rrh.end(), a[i]) - rrh.begin() + 1;
fwd[i] = query(1, 1, rrh.size(), 1, r) + 1;
update(1, 1, rrh.size(), r, fwd[i]);
}
memset(f, 0, sizeof f);
for (int i = n; i >= 1; --i) {
int l = lower_bound(rrh.begin(), rrh.end(), a[i]) - rrh.begin() + 1;
bwd[i] = query(1, 1, rrh.size(), l + 1, rrh.size()) + 1;
update(1, 1, rrh.size(), l, bwd[i]);
}
memset(f, 0, sizeof f);
int answer = 0;
for (int i = 1; i <= n; ++i) {
int it = upper_bound(rrh.begin(), rrh.end(), x - a[i]) - rrh.begin();
answer = max(answer, bwd[i] + query(1, 1, rrh.size(), 1, it));
int r = lower_bound(rrh.begin(), rrh.end(), a[i]) - rrh.begin() + 1;
update(1, 1, rrh.size(), r, fwd[i]);
}
memset(f, 0, sizeof f);
for (int i = n; i >= 1; --i) {
int it = upper_bound(rrh.begin(), rrh.end(), a[i] - x) - rrh.begin() + 1;
answer = max(answer, fwd[i] + query(1, 1, rrh.size(), it, rrh.size()));
int l = lower_bound(rrh.begin(), rrh.end(), a[i]) - rrh.begin() + 1;
update(1, 1, rrh.size(), l, bwd[i]);
}
cout << answer << "\n";
}
Compilation message
glo.cpp: In function 'void update(int, int, int, int, int)':
glo.cpp:18:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | int mid = l + r >> 1;
| ~~^~~
glo.cpp: In function 'int query(int, int, int, int, int)':
glo.cpp:25:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
382 ms |
6652 KB |
Output is correct |
2 |
Correct |
377 ms |
6480 KB |
Output is correct |
3 |
Correct |
375 ms |
6644 KB |
Output is correct |
4 |
Correct |
392 ms |
6548 KB |
Output is correct |
5 |
Incorrect |
188 ms |
6484 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
4292 KB |
Output is correct |
2 |
Correct |
85 ms |
4188 KB |
Output is correct |
3 |
Correct |
81 ms |
4184 KB |
Output is correct |
4 |
Incorrect |
44 ms |
4184 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
5172 KB |
Output is correct |
2 |
Correct |
184 ms |
4944 KB |
Output is correct |
3 |
Correct |
380 ms |
6604 KB |
Output is correct |
4 |
Incorrect |
178 ms |
6564 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |