#include <cmath>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <cassert>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
using namespace std;
template<class T>
class SegmentTree {
public:
SegmentTree (int N) {
N = (1 << ((int)floor(log2(N - 1)) + 1));
this->N = N;
val.assign(2 * N, ID);
}
void update (int x, T y) {
x += N - 1;
val[x] = y;
while (x != 0) {
x = (x - 1)/2;
val[x] = merge(val[2 * x + 1], val[2 * x + 2]);
}
}
T query (int ind, const int l, const int r, int tl, int tr) {
if (tl >= l && tr <= r) {
return val[ind];
}
if (tr < l || tl > r) {
return ID;
}
return merge(query(2 * ind + 1, l, r, tl, (tl + tr)/2), query(2 * ind + 2, l, r, (tl + tr)/2 + 1, tr));
}
T query (int l, int r) {
return query(0, l, r, 0, N - 1);
}
private:
vector<T> val;
T ID = 0;
T merge (T x, T y) {
return max(x, y);
}
int N;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64_t N, D; cin >> N >> D;
vector<int64_t> arr(N); set<int64_t> s;
for (int i = 0; i < N; i++) {
cin >> arr[i]; s.insert(arr[i]), s.insert(arr[i] + D);
}
map<int64_t,int> myMap; int cntr = 1;
for (int64_t i: s) myMap[i] = cntr++;
cntr++;
SegmentTree<int> st1(cntr);
vector<int> remove(N);
for (int i = 0; i < N; i++) {
remove[i] = st1.query(myMap[arr[i]], myMap[arr[i]]);
st1.update(myMap[arr[i]], st1.query(0, myMap[arr[i]] - 1) + 1);
}
SegmentTree<int> st2(cntr);
vector<int> v;
for (int i = N - 1; i >= 0; i--) {
st1.update(myMap[arr[i]], remove[i]), st2.update(myMap[arr[i] + D], st2.query(myMap[arr[i] + D] + 1, cntr - 1) + 1);
v.push_back(st1.query(0, myMap[D + arr[i]] - 1) + st2.query(myMap[D + arr[i]] + 1, cntr - 1) + 1);
}
int myMax = 0;
for (int i: v) {
myMax = max(myMax, i);
}
cout << myMax;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
368 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
368 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
4 ms |
616 KB |
Output is correct |
20 |
Correct |
4 ms |
584 KB |
Output is correct |
21 |
Correct |
4 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
3 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1402 ms |
30468 KB |
Output is correct |
2 |
Correct |
1314 ms |
30484 KB |
Output is correct |
3 |
Correct |
1321 ms |
30468 KB |
Output is correct |
4 |
Correct |
1269 ms |
30464 KB |
Output is correct |
5 |
Correct |
519 ms |
17384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
14776 KB |
Output is correct |
2 |
Correct |
312 ms |
14848 KB |
Output is correct |
3 |
Correct |
305 ms |
14740 KB |
Output is correct |
4 |
Correct |
161 ms |
8088 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
154 ms |
8072 KB |
Output is correct |
7 |
Correct |
218 ms |
12940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
732 ms |
28204 KB |
Output is correct |
2 |
Correct |
793 ms |
28292 KB |
Output is correct |
3 |
Correct |
1957 ms |
55848 KB |
Output is correct |
4 |
Correct |
661 ms |
30204 KB |
Output is correct |
5 |
Correct |
391 ms |
28476 KB |
Output is correct |
6 |
Correct |
710 ms |
53916 KB |
Output is correct |
7 |
Correct |
715 ms |
53904 KB |
Output is correct |
8 |
Correct |
486 ms |
28920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
368 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
4 ms |
616 KB |
Output is correct |
20 |
Correct |
4 ms |
584 KB |
Output is correct |
21 |
Correct |
4 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
3 ms |
448 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1402 ms |
30468 KB |
Output is correct |
28 |
Correct |
1314 ms |
30484 KB |
Output is correct |
29 |
Correct |
1321 ms |
30468 KB |
Output is correct |
30 |
Correct |
1269 ms |
30464 KB |
Output is correct |
31 |
Correct |
519 ms |
17384 KB |
Output is correct |
32 |
Correct |
330 ms |
14776 KB |
Output is correct |
33 |
Correct |
312 ms |
14848 KB |
Output is correct |
34 |
Correct |
305 ms |
14740 KB |
Output is correct |
35 |
Correct |
161 ms |
8088 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
154 ms |
8072 KB |
Output is correct |
38 |
Correct |
218 ms |
12940 KB |
Output is correct |
39 |
Correct |
732 ms |
28204 KB |
Output is correct |
40 |
Correct |
793 ms |
28292 KB |
Output is correct |
41 |
Correct |
1957 ms |
55848 KB |
Output is correct |
42 |
Correct |
661 ms |
30204 KB |
Output is correct |
43 |
Correct |
391 ms |
28476 KB |
Output is correct |
44 |
Correct |
710 ms |
53916 KB |
Output is correct |
45 |
Correct |
715 ms |
53904 KB |
Output is correct |
46 |
Correct |
486 ms |
28920 KB |
Output is correct |
47 |
Correct |
745 ms |
29020 KB |
Output is correct |
48 |
Correct |
693 ms |
28960 KB |
Output is correct |
49 |
Correct |
1745 ms |
56748 KB |
Output is correct |
50 |
Correct |
558 ms |
30660 KB |
Output is correct |
51 |
Correct |
508 ms |
24588 KB |
Output is correct |
52 |
Correct |
612 ms |
31140 KB |
Output is correct |
53 |
Correct |
777 ms |
57040 KB |
Output is correct |
54 |
Correct |
824 ms |
57752 KB |
Output is correct |
55 |
Correct |
1112 ms |
50596 KB |
Output is correct |