#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 5;
int a[maxN], num[maxN];
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, add;
cin >> n >> add;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> LIS = {a[1]}; num[1] = 1;
for (int i = 2; i <= n; i++) {
if (LIS.back() < a[i]) {
LIS.push_back(a[i]);
num[i] = LIS.size();
continue;
}
int x = lower_bound(LIS.begin(), LIS.end(), a[i]) - LIS.begin();
num[i] = x + 1; LIS[x] = a[i];
}
int ans = LIS.size();
LIS.clear(); LIS.push_back(a[n]);
for (int i = n - 1; i >= 1; i--) {
int low = 0, high = LIS.size();
while (low < high) {
int mid = (low + high + 1) / 2;
if (LIS[mid - 1] > a[i] - add) low = mid;
else high = mid - 1;
}
ans = max(ans, num[i] + low);
if (LIS.back() > a[i]) {
LIS.push_back(a[i]);
continue;
}
low = 0, high = LIS.size() - 1;
while (low < high) {
int mid = (low + high) / 2;
if (LIS[mid] <= a[i]) high = mid;
else low = mid + 1;
}
LIS[low] = a[i];
}
cout << ans;
return 0;
}
/*
50 15
10 9 8 7 1 3 6 7 1 8 2 2 8 8 2 10 1 10 9 4 9 2 5 6 2 2 4 7 7 4 4 7 7 9 6 5 8 7 7 4 8 9 6 6 10 1 3 2 8 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
0 ms |
360 KB |
Output is correct |
14 |
Correct |
0 ms |
360 KB |
Output is correct |
15 |
Correct |
0 ms |
356 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
0 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
0 ms |
360 KB |
Output is correct |
14 |
Correct |
0 ms |
360 KB |
Output is correct |
15 |
Correct |
0 ms |
356 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
0 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
0 ms |
360 KB |
Output is correct |
21 |
Correct |
0 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
0 ms |
360 KB |
Output is correct |
24 |
Correct |
0 ms |
360 KB |
Output is correct |
25 |
Correct |
0 ms |
360 KB |
Output is correct |
26 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
3936 KB |
Output is correct |
2 |
Correct |
36 ms |
3936 KB |
Output is correct |
3 |
Correct |
39 ms |
3920 KB |
Output is correct |
4 |
Correct |
38 ms |
3688 KB |
Output is correct |
5 |
Correct |
25 ms |
3552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1116 KB |
Output is correct |
2 |
Correct |
10 ms |
1352 KB |
Output is correct |
3 |
Correct |
9 ms |
1116 KB |
Output is correct |
4 |
Correct |
6 ms |
1372 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1268 KB |
Output is correct |
7 |
Correct |
8 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2136 KB |
Output is correct |
2 |
Correct |
18 ms |
2140 KB |
Output is correct |
3 |
Correct |
35 ms |
3788 KB |
Output is correct |
4 |
Correct |
25 ms |
3552 KB |
Output is correct |
5 |
Correct |
16 ms |
2088 KB |
Output is correct |
6 |
Correct |
18 ms |
3552 KB |
Output is correct |
7 |
Correct |
26 ms |
4320 KB |
Output is correct |
8 |
Correct |
15 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
0 ms |
360 KB |
Output is correct |
14 |
Correct |
0 ms |
360 KB |
Output is correct |
15 |
Correct |
0 ms |
356 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
0 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
0 ms |
360 KB |
Output is correct |
21 |
Correct |
0 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
0 ms |
360 KB |
Output is correct |
24 |
Correct |
0 ms |
360 KB |
Output is correct |
25 |
Correct |
0 ms |
360 KB |
Output is correct |
26 |
Correct |
0 ms |
360 KB |
Output is correct |
27 |
Correct |
36 ms |
3936 KB |
Output is correct |
28 |
Correct |
36 ms |
3936 KB |
Output is correct |
29 |
Correct |
39 ms |
3920 KB |
Output is correct |
30 |
Correct |
38 ms |
3688 KB |
Output is correct |
31 |
Correct |
25 ms |
3552 KB |
Output is correct |
32 |
Correct |
9 ms |
1116 KB |
Output is correct |
33 |
Correct |
10 ms |
1352 KB |
Output is correct |
34 |
Correct |
9 ms |
1116 KB |
Output is correct |
35 |
Correct |
6 ms |
1372 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
6 ms |
1268 KB |
Output is correct |
38 |
Correct |
8 ms |
1116 KB |
Output is correct |
39 |
Correct |
17 ms |
2136 KB |
Output is correct |
40 |
Correct |
18 ms |
2140 KB |
Output is correct |
41 |
Correct |
35 ms |
3788 KB |
Output is correct |
42 |
Correct |
25 ms |
3552 KB |
Output is correct |
43 |
Correct |
16 ms |
2088 KB |
Output is correct |
44 |
Correct |
18 ms |
3552 KB |
Output is correct |
45 |
Correct |
26 ms |
4320 KB |
Output is correct |
46 |
Correct |
15 ms |
2140 KB |
Output is correct |
47 |
Correct |
17 ms |
2180 KB |
Output is correct |
48 |
Correct |
17 ms |
2176 KB |
Output is correct |
49 |
Correct |
38 ms |
3920 KB |
Output is correct |
50 |
Correct |
25 ms |
3552 KB |
Output is correct |
51 |
Correct |
20 ms |
3036 KB |
Output is correct |
52 |
Correct |
26 ms |
3804 KB |
Output is correct |
53 |
Correct |
17 ms |
3804 KB |
Output is correct |
54 |
Correct |
26 ms |
4452 KB |
Output is correct |
55 |
Correct |
32 ms |
3932 KB |
Output is correct |