#include <bits/stdc++.h>
#define all(d) begin(d), end(d)
using namespace std;
using ll = long long;
struct SegTree {
using T = ll;
T f(T a, T b) { return max(a, b); }
static constexpr T UNIT = 0;
vector<T> s; ll n;
SegTree(ll len) : s(2 * len, UNIT), n(len) {}
void set(ll pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(ll lo, ll hi) { // query lo to hi (hi not included)
T ra = UNIT, rb = UNIT;
for (lo += n, hi += n; lo < hi; lo /= 2, hi /= 2) {
if (lo % 2) ra = f(ra, s[lo++]);
if (hi % 2) rb = f(s[--hi], rb);
}
return f(ra, rb);
}
};
int main() {
ll n, d;
cin >> n >> d;
vector<ll> a(n);
for (ll i = 0; i < n; i++)
cin >> a[i];
vector<ll> distinctA = a;
distinctA.push_back(LLONG_MIN);
distinctA.push_back(LLONG_MAX);
sort(all(distinctA));
distinctA.erase(unique(all(distinctA)), distinctA.end());
vector<array<ll, 2>> dp(n);
ll ans = 0;
vector<SegTree> st;
st.emplace_back(distinctA.size());
st.emplace_back(distinctA.size());
for (ll i = n - 1; i >= 0; i--) {
ll lo1 = upper_bound(all(distinctA), a[i]) - distinctA.begin();
for (ll u = 0; u < 2; u++)
dp[i][u] = st[u].query(lo1, distinctA.size()) + 1;
ll lo2 = upper_bound(all(distinctA), a[i] - d) - distinctA.begin();
dp[i][1] = max(dp[i][1], st[0].query(lo2, distinctA.size()) + 1);
for (ll u = 0; u < 2; u++)
st[u].set(lo1 - 1, dp[i][u]);
ans = max(ans, dp[i][1]);
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
252 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
252 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
252 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
4 ms |
376 KB |
Output is correct |
21 |
Correct |
4 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
14784 KB |
Output is correct |
2 |
Correct |
387 ms |
14812 KB |
Output is correct |
3 |
Correct |
397 ms |
14812 KB |
Output is correct |
4 |
Correct |
399 ms |
14940 KB |
Output is correct |
5 |
Correct |
194 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
4068 KB |
Output is correct |
2 |
Correct |
82 ms |
3952 KB |
Output is correct |
3 |
Correct |
81 ms |
3952 KB |
Output is correct |
4 |
Correct |
48 ms |
3028 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
47 ms |
2904 KB |
Output is correct |
7 |
Correct |
82 ms |
3952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
7528 KB |
Output is correct |
2 |
Correct |
161 ms |
7656 KB |
Output is correct |
3 |
Correct |
354 ms |
14848 KB |
Output is correct |
4 |
Correct |
182 ms |
10840 KB |
Output is correct |
5 |
Correct |
103 ms |
7224 KB |
Output is correct |
6 |
Correct |
206 ms |
13608 KB |
Output is correct |
7 |
Correct |
228 ms |
14120 KB |
Output is correct |
8 |
Correct |
144 ms |
7656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
252 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
3 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
4 ms |
376 KB |
Output is correct |
21 |
Correct |
4 ms |
376 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
3 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
376 KB |
Output is correct |
27 |
Correct |
393 ms |
14784 KB |
Output is correct |
28 |
Correct |
387 ms |
14812 KB |
Output is correct |
29 |
Correct |
397 ms |
14812 KB |
Output is correct |
30 |
Correct |
399 ms |
14940 KB |
Output is correct |
31 |
Correct |
194 ms |
10844 KB |
Output is correct |
32 |
Correct |
99 ms |
4068 KB |
Output is correct |
33 |
Correct |
82 ms |
3952 KB |
Output is correct |
34 |
Correct |
81 ms |
3952 KB |
Output is correct |
35 |
Correct |
48 ms |
3028 KB |
Output is correct |
36 |
Correct |
2 ms |
256 KB |
Output is correct |
37 |
Correct |
47 ms |
2904 KB |
Output is correct |
38 |
Correct |
82 ms |
3952 KB |
Output is correct |
39 |
Correct |
153 ms |
7528 KB |
Output is correct |
40 |
Correct |
161 ms |
7656 KB |
Output is correct |
41 |
Correct |
354 ms |
14848 KB |
Output is correct |
42 |
Correct |
182 ms |
10840 KB |
Output is correct |
43 |
Correct |
103 ms |
7224 KB |
Output is correct |
44 |
Correct |
206 ms |
13608 KB |
Output is correct |
45 |
Correct |
228 ms |
14120 KB |
Output is correct |
46 |
Correct |
144 ms |
7656 KB |
Output is correct |
47 |
Correct |
176 ms |
7652 KB |
Output is correct |
48 |
Correct |
173 ms |
7528 KB |
Output is correct |
49 |
Correct |
395 ms |
14828 KB |
Output is correct |
50 |
Correct |
190 ms |
11072 KB |
Output is correct |
51 |
Correct |
155 ms |
9228 KB |
Output is correct |
52 |
Correct |
229 ms |
14172 KB |
Output is correct |
53 |
Correct |
218 ms |
14300 KB |
Output is correct |
54 |
Correct |
242 ms |
14940 KB |
Output is correct |
55 |
Correct |
320 ms |
14912 KB |
Output is correct |