#include <bits/stdc++.h>
using namespace std;
void maxSelf(int &x, int y) {
if (x < y) {
x = y;
}
}
struct ST {
int n;
vector<int> t;
ST(int N) : n(N) {
int dim = 1;
while (dim < n) {
dim *= 2;
}
t.resize(dim * 2);
}
void reset() {
for (int &it : t) {
it = 0;
}
}
void update(int x, int lx, int rx, int pos, int v) {
if (lx == rx) {
t[x] = v;
return;
}
int mid = (lx + rx) / 2;
if (pos <= mid) {
update(x * 2, lx, mid, pos, v);
} else {
update(x * 2 + 1, mid + 1, rx, pos, v);
}
t[x] = max(t[x * 2], t[x * 2 + 1]);
}
void update(int pos, int v) {
update(1, 0, n - 1, pos, v);
}
int query(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
return t[x];
}
int mid = (lx + rx) / 2, ans = 0;
if (st <= mid) {
maxSelf(ans, query(x * 2, lx, mid, st, dr));
}
if (mid < dr) {
maxSelf(ans, query(x * 2 + 1, mid + 1, rx, st, dr));
}
return ans;
}
int query(int st, int dr) {
if (dr < st) {
return 0;
}
return query(1, 0, n - 1, st, dr);
}
};
void testCase() {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int &x : a) {
cin >> x;
}
vector<int> v = a;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int m = v.size();
ST t(m);
vector<int> dp(n);
auto getIndex = [&](const int &val) -> int {
return distance(v.begin(), lower_bound(v.begin(), v.end(), val));
};
for (int i = n - 1; i >= 0; --i) {
int index = getIndex(a[i]);
dp[i] = t.query(index + 1, m - 1) + 1;
t.update(index, dp[i]);
}
t.reset();
int ans = 0;
for (int i = 0; i < n; ++i) {
maxSelf(ans, t.query(0, getIndex(a[i] + x) - 1) + dp[i]);
a[i] = getIndex(a[i]);
int best = t.query(0, a[i] - 1) + 1;
t.update(a[i], best);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 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 |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 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 |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
216 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 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 |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
216 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
4716 KB |
Output is correct |
2 |
Correct |
259 ms |
4820 KB |
Output is correct |
3 |
Correct |
255 ms |
4688 KB |
Output is correct |
4 |
Correct |
243 ms |
4836 KB |
Output is correct |
5 |
Correct |
149 ms |
3660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
1412 KB |
Output is correct |
2 |
Correct |
51 ms |
1416 KB |
Output is correct |
3 |
Correct |
51 ms |
1356 KB |
Output is correct |
4 |
Correct |
26 ms |
1160 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
28 ms |
1100 KB |
Output is correct |
7 |
Correct |
49 ms |
1904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
2520 KB |
Output is correct |
2 |
Correct |
93 ms |
2516 KB |
Output is correct |
3 |
Correct |
199 ms |
4676 KB |
Output is correct |
4 |
Correct |
97 ms |
3660 KB |
Output is correct |
5 |
Correct |
61 ms |
2512 KB |
Output is correct |
6 |
Correct |
111 ms |
4596 KB |
Output is correct |
7 |
Correct |
106 ms |
4516 KB |
Output is correct |
8 |
Correct |
76 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 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 |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
216 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
277 ms |
4716 KB |
Output is correct |
28 |
Correct |
259 ms |
4820 KB |
Output is correct |
29 |
Correct |
255 ms |
4688 KB |
Output is correct |
30 |
Correct |
243 ms |
4836 KB |
Output is correct |
31 |
Correct |
149 ms |
3660 KB |
Output is correct |
32 |
Correct |
53 ms |
1412 KB |
Output is correct |
33 |
Correct |
51 ms |
1416 KB |
Output is correct |
34 |
Correct |
51 ms |
1356 KB |
Output is correct |
35 |
Correct |
26 ms |
1160 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
28 ms |
1100 KB |
Output is correct |
38 |
Correct |
49 ms |
1904 KB |
Output is correct |
39 |
Correct |
119 ms |
2520 KB |
Output is correct |
40 |
Correct |
93 ms |
2516 KB |
Output is correct |
41 |
Correct |
199 ms |
4676 KB |
Output is correct |
42 |
Correct |
97 ms |
3660 KB |
Output is correct |
43 |
Correct |
61 ms |
2512 KB |
Output is correct |
44 |
Correct |
111 ms |
4596 KB |
Output is correct |
45 |
Correct |
106 ms |
4516 KB |
Output is correct |
46 |
Correct |
76 ms |
2516 KB |
Output is correct |
47 |
Correct |
116 ms |
3380 KB |
Output is correct |
48 |
Correct |
112 ms |
3388 KB |
Output is correct |
49 |
Correct |
261 ms |
6644 KB |
Output is correct |
50 |
Correct |
116 ms |
4784 KB |
Output is correct |
51 |
Correct |
102 ms |
3972 KB |
Output is correct |
52 |
Correct |
159 ms |
5936 KB |
Output is correct |
53 |
Correct |
109 ms |
5944 KB |
Output is correct |
54 |
Correct |
115 ms |
6592 KB |
Output is correct |
55 |
Correct |
220 ms |
6584 KB |
Output is correct |