#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 200200
int n , x , p , fp , m , rt , l , r , mid , a[N] , b[N] , c[N] , d[N] , it[N << 2];
vector <int> vi;
void upd(int i,int l,int r,int p,int x){
if (r < p || p < l) return;
if (l == r) return it[i] = x, void();
int mid = (l + r) >> 1;
if (p <= mid) upd(i << 1,l,mid,p,x); else upd(i << 1|1,mid + 1,r,p,x);
it[i] = max(it[i << 1] , it[i << 1|1]);
}
int get(int i,int l,int r,int u,int v){
if (v < l || r < u) return 0;
if (u <= l && r <= v) return it[i];
int mid = (l + r) >> 1;
return max(get(i << 1,l,mid,u,v) , get(i << 1|1,mid + 1,r,u,v));
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> x;
memset(d , 0x3f , sizeof d);
for (int i = 1 ; i <= n ; i ++){
cin >> a[i] , b[i] = lower_bound(d + 1,d + n + 1 , a[i]) - d; d[b[i]] = a[i];
vi.push_back(a[i]) , vi.push_back(a[i] + x) , vi.push_back(a[i] - x);
}
memset(d , 0 , sizeof d);
for (int i = n ; i ; i --){
l = 1 , r = n;
while (l <= r){
mid = (l + r) >> 1;
if (d[mid] <= a[i]) p = mid , r = mid - 1; else l = mid + 1;
}
c[i] = p , d[p] = a[i];
}
sort(vi.begin() , vi.end());
vi.resize(unique(vi.begin() , vi.end()) - vi.begin());
m = vi.size();
for (int i = n ; i ; i --){
p = lower_bound(vi.begin() , vi.end() , a[i]) - vi.begin() + 1 , fp = get(1 , 1 , m , p + 1 , m);
rt = max(rt ,fp + b[i]);
p = lower_bound(vi.begin() , vi.end() , a[i] + x) - vi.begin() + 1 , fp = get(1 , 1 , m , p + 1 , m);
p = lower_bound(vi.begin() , vi.end() , a[i] + x) - vi.begin() + 1 , upd(1 , 1 , m , p , fp + 1);
}
memset(it , 0 , sizeof it);
for (int i = n ; i ; i --){
p = lower_bound(vi.begin() , vi.end() , a[i]) - vi.begin() + 1 , fp = get(1 , 1 , m , p + 1 , m);
rt = max(rt ,fp + b[i]);
p = lower_bound(vi.begin() , vi.end() , a[i] - x) - vi.begin() + 1 , fp = get(1 , 1 , m , p + 1 , m);
p = lower_bound(vi.begin() , vi.end() , a[i] - x) - vi.begin() + 1 , upd(1 , 1 , m , p , fp + 1);
}
memset(it , 0 , sizeof it);
for (int i = 1 ; i <= n ; i ++){
p = lower_bound(vi.begin() , vi.end() , a[i]) - vi.begin() + 1 , fp = get(1 , 1 , m , 1 , p - 1);
rt = max(rt ,fp + c[i]);
p = lower_bound(vi.begin() , vi.end() , a[i] - x) - vi.begin() + 1 , fp = get(1 , 1 , m , 1 , p - 1);
p = lower_bound(vi.begin() , vi.end() , a[i] - x) - vi.begin() + 1 , upd(1 , 1 , m , p , fp + 1);
}
memset(it , 0 , sizeof it);
for (int i = 1 ; i <= n ; i ++){
p = lower_bound(vi.begin() , vi.end() , a[i]) - vi.begin() + 1 , fp = get(1 , 1 , m , 1 , p - 1);
rt = max(rt ,fp + c[i]);
p = lower_bound(vi.begin() , vi.end() , a[i] + x) - vi.begin() + 1 , fp = get(1 , 1 , m , 1 , p - 1);
p = lower_bound(vi.begin() , vi.end() , a[i] + x) - vi.begin() + 1 , upd(1 , 1 , m , p , fp + 1);
}
cout << rt << '\n';
return 0;
}
/*
8 10
7 3 5 12 2 7 3 4
2
3
4
5
7
12
13
14
15
17
22
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4340 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4184 KB |
Output is correct |
11 |
Correct |
3 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4340 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4184 KB |
Output is correct |
11 |
Correct |
3 ms |
4184 KB |
Output is correct |
12 |
Correct |
2 ms |
4184 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
3 ms |
4184 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
2 ms |
4316 KB |
Output is correct |
18 |
Correct |
2 ms |
4188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4340 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4184 KB |
Output is correct |
11 |
Correct |
3 ms |
4184 KB |
Output is correct |
12 |
Correct |
2 ms |
4184 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
3 ms |
4184 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
2 ms |
4316 KB |
Output is correct |
18 |
Correct |
2 ms |
4188 KB |
Output is correct |
19 |
Correct |
5 ms |
4444 KB |
Output is correct |
20 |
Correct |
4 ms |
4444 KB |
Output is correct |
21 |
Correct |
4 ms |
4444 KB |
Output is correct |
22 |
Correct |
4 ms |
4444 KB |
Output is correct |
23 |
Correct |
4 ms |
4416 KB |
Output is correct |
24 |
Correct |
4 ms |
4444 KB |
Output is correct |
25 |
Correct |
3 ms |
4444 KB |
Output is correct |
26 |
Correct |
4 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
574 ms |
10940 KB |
Output is correct |
2 |
Correct |
557 ms |
10944 KB |
Output is correct |
3 |
Correct |
564 ms |
10944 KB |
Output is correct |
4 |
Correct |
577 ms |
11080 KB |
Output is correct |
5 |
Correct |
271 ms |
10176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
6096 KB |
Output is correct |
2 |
Correct |
146 ms |
6088 KB |
Output is correct |
3 |
Correct |
159 ms |
5964 KB |
Output is correct |
4 |
Correct |
73 ms |
5844 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
79 ms |
5848 KB |
Output is correct |
7 |
Correct |
119 ms |
6096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
335 ms |
7788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
2 ms |
4188 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
2 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4340 KB |
Output is correct |
8 |
Correct |
2 ms |
4188 KB |
Output is correct |
9 |
Correct |
2 ms |
4188 KB |
Output is correct |
10 |
Correct |
2 ms |
4184 KB |
Output is correct |
11 |
Correct |
3 ms |
4184 KB |
Output is correct |
12 |
Correct |
2 ms |
4184 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
3 ms |
4184 KB |
Output is correct |
15 |
Correct |
2 ms |
4188 KB |
Output is correct |
16 |
Correct |
2 ms |
4188 KB |
Output is correct |
17 |
Correct |
2 ms |
4316 KB |
Output is correct |
18 |
Correct |
2 ms |
4188 KB |
Output is correct |
19 |
Correct |
5 ms |
4444 KB |
Output is correct |
20 |
Correct |
4 ms |
4444 KB |
Output is correct |
21 |
Correct |
4 ms |
4444 KB |
Output is correct |
22 |
Correct |
4 ms |
4444 KB |
Output is correct |
23 |
Correct |
4 ms |
4416 KB |
Output is correct |
24 |
Correct |
4 ms |
4444 KB |
Output is correct |
25 |
Correct |
3 ms |
4444 KB |
Output is correct |
26 |
Correct |
4 ms |
4440 KB |
Output is correct |
27 |
Correct |
574 ms |
10940 KB |
Output is correct |
28 |
Correct |
557 ms |
10944 KB |
Output is correct |
29 |
Correct |
564 ms |
10944 KB |
Output is correct |
30 |
Correct |
577 ms |
11080 KB |
Output is correct |
31 |
Correct |
271 ms |
10176 KB |
Output is correct |
32 |
Correct |
144 ms |
6096 KB |
Output is correct |
33 |
Correct |
146 ms |
6088 KB |
Output is correct |
34 |
Correct |
159 ms |
5964 KB |
Output is correct |
35 |
Correct |
73 ms |
5844 KB |
Output is correct |
36 |
Correct |
2 ms |
4188 KB |
Output is correct |
37 |
Correct |
79 ms |
5848 KB |
Output is correct |
38 |
Correct |
119 ms |
6096 KB |
Output is correct |
39 |
Incorrect |
335 ms |
7788 KB |
Output isn't correct |
40 |
Halted |
0 ms |
0 KB |
- |