#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
const int MOD = 1e9 + 7;
template<typename T>
struct segTree{
vector<T> t;
int N;
segTree(int n) : N(1){
while(N < n) N <<= 1;
t.resize(N << 1);
}
segTree(const vector<T> &vec) : segTree(vec.size()){
for(int i = 0; i < (int)vec.size(); i++){
t[i + N] = vec[i];
}
for(int i = N - 1; i > 0; i--){
t[i] = max(t[i << 1], t[i << 1 | 1]);
}
}
void upd(int i, T val){
assert(i >= 0 && i < N);
i += N;
t[i] = t[i] + val;
while(i >>= 1){
t[i] = max(t[i << 1], t[i << 1 | 1]);
}
}
T clc(int l, int r, int i, int from, int to){
if(from > r || to < l){
return T();
}
if(from >= l && to <= r){
return t[i];
}
int mid = (from + to) >> 1;
return max(clc(l, r, i << 1, from, mid), clc(l, r, i << 1 | 1, mid + 1, to));
}
T clc(int l, int r){
assert(l >= 0 && l <= r && r < N);
return clc(l, r, 1, 0, N - 1);
}
};
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, x;
cin >> n >> x;
int a[n];
for(int &i : a){
cin >> i;
}
int l[n], r[n];
int dp[n];
memset(dp, INF, sizeof dp);
for(int i = 0; i < n; i++){
int x = lower_bound(dp, dp + n, a[i]) - dp;
l[i] = x + 1;
dp[x] = a[i];
}
memset(dp, INF, sizeof dp);
for(int i = n - 1; i >= 0; i--){
int x = lower_bound(dp, dp + n, -a[i]) - dp;
r[i] = x + 1;
dp[x] = -a[i];
}
vector<int> vec(n);
iota(all(vec), 0);
sort(all(vec), [&](int x, int y){
return a[x] > a[y];
});
int ans = 0;
segTree<int> L(n), R(n);
for(int i = 0, j = 0; i < n; i++){
int idx = vec[i];
while(j < i && a[vec[j]] - a[idx] >= x){
L.upd(vec[j], -l[vec[j]]);
R.upd(vec[j], -r[vec[j]]);
j++;
}
ans = max(ans, l[idx] + R.clc(idx, n - 1));
ans = max(ans, L.clc(0, idx) + r[idx]);
L.upd(idx, l[idx]);
R.upd(idx, r[idx]);
}
cout << ans << endl;
return 0;
}
// by me
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
10320 KB |
Output is correct |
2 |
Correct |
153 ms |
10320 KB |
Output is correct |
3 |
Correct |
152 ms |
10376 KB |
Output is correct |
4 |
Correct |
157 ms |
10296 KB |
Output is correct |
5 |
Correct |
117 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
2904 KB |
Output is correct |
2 |
Correct |
34 ms |
2956 KB |
Output is correct |
3 |
Correct |
36 ms |
2904 KB |
Output is correct |
4 |
Correct |
25 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
25 ms |
2652 KB |
Output is correct |
7 |
Correct |
30 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
5424 KB |
Output is correct |
2 |
Correct |
65 ms |
5420 KB |
Output is correct |
3 |
Correct |
139 ms |
10352 KB |
Output is correct |
4 |
Correct |
104 ms |
9552 KB |
Output is correct |
5 |
Correct |
47 ms |
5056 KB |
Output is correct |
6 |
Correct |
92 ms |
9296 KB |
Output is correct |
7 |
Correct |
95 ms |
10068 KB |
Output is correct |
8 |
Correct |
55 ms |
5436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
157 ms |
10320 KB |
Output is correct |
28 |
Correct |
153 ms |
10320 KB |
Output is correct |
29 |
Correct |
152 ms |
10376 KB |
Output is correct |
30 |
Correct |
157 ms |
10296 KB |
Output is correct |
31 |
Correct |
117 ms |
9812 KB |
Output is correct |
32 |
Correct |
34 ms |
2904 KB |
Output is correct |
33 |
Correct |
34 ms |
2956 KB |
Output is correct |
34 |
Correct |
36 ms |
2904 KB |
Output is correct |
35 |
Correct |
25 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
25 ms |
2652 KB |
Output is correct |
38 |
Correct |
30 ms |
2904 KB |
Output is correct |
39 |
Correct |
65 ms |
5424 KB |
Output is correct |
40 |
Correct |
65 ms |
5420 KB |
Output is correct |
41 |
Correct |
139 ms |
10352 KB |
Output is correct |
42 |
Correct |
104 ms |
9552 KB |
Output is correct |
43 |
Correct |
47 ms |
5056 KB |
Output is correct |
44 |
Correct |
92 ms |
9296 KB |
Output is correct |
45 |
Correct |
95 ms |
10068 KB |
Output is correct |
46 |
Correct |
55 ms |
5436 KB |
Output is correct |
47 |
Correct |
81 ms |
5424 KB |
Output is correct |
48 |
Correct |
90 ms |
5168 KB |
Output is correct |
49 |
Correct |
159 ms |
10296 KB |
Output is correct |
50 |
Correct |
109 ms |
9612 KB |
Output is correct |
51 |
Correct |
86 ms |
8272 KB |
Output is correct |
52 |
Correct |
116 ms |
9552 KB |
Output is correct |
53 |
Correct |
97 ms |
9552 KB |
Output is correct |
54 |
Correct |
98 ms |
10320 KB |
Output is correct |
55 |
Correct |
135 ms |
10292 KB |
Output is correct |