#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=6e5+5;
const int inf=1e9;
vector<int> v;
struct BIT1 {
int bit[mxn] = {};
void update(int p,int v) {
for(;p<mxn;p+=p&-p) bit[p]=max(bit[p],v);
}
int query(int p,int r = 0) {
for(;p>0;p-=p&-p) r=max(r,bit[p]);
return r;
}
} inc,bt1;
struct BIT2 {
int bit[mxn] = {};
void update(int p,int v) {
for(;p>0;p-=p&-p) bit[p]=max(bit[p],v);
}
int query(int p,int r = 0) {
for(;p<mxn;p+=p&-p) r=max(r,bit[p]);
return r;
}
} dc,bt2;
int get(int x) {
return lower_bound(v.begin(),v.end(),x) - v.begin() + 1;
}
int main() {
int n,x;
cin>>n>>x;
int a[n+1];
for(int i = 1; i <= n; i++) {
cin>>a[i];
v.push_back(a[i]);
v.push_back(a[i] - x);
v.push_back(a[i] + x);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
int dp1[mxn],dp2[mxn];
int ans = 0;
for(int i = 1; i <= n; i++) {
int p = get(a[i]);
int x = inc.query(p - 1);
dp1[i] = x + 1;
ans = max(ans,dp1[i]);
inc.update(p,dp1[i]);
}
for(int i = n; i > 0 ; i--) {
int p = get(a[i]);
int x = dc.query(p + 1);
dp2[i] = x + 1;
dc.update(p,dp2[i]);
}
for(int i = 1; i <= n; i++) {
int ind = get(a[i]);
int v = bt1.query(ind - 1);
ans = max(ans, v + dp2[i]);
bt1.update(get(a[i] - x), dp1[i]);
}
for(int i = n; i >= 1; i--) {
int ind = get(a[i]);
int v = bt2.query(ind + 1);
ans = max(ans, v + dp1[i]);
bt1.update(get(a[i] + x), dp2[i]);
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
430 ms |
7516 KB |
Output is correct |
2 |
Correct |
433 ms |
7644 KB |
Output is correct |
3 |
Correct |
427 ms |
7644 KB |
Output is correct |
4 |
Correct |
433 ms |
7644 KB |
Output is correct |
5 |
Correct |
232 ms |
6364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
3432 KB |
Output is correct |
2 |
Correct |
120 ms |
3436 KB |
Output is correct |
3 |
Correct |
108 ms |
3432 KB |
Output is correct |
4 |
Correct |
59 ms |
2540 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
60 ms |
2284 KB |
Output is correct |
7 |
Correct |
99 ms |
3016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
221 ms |
4452 KB |
Output is correct |
2 |
Correct |
221 ms |
4596 KB |
Output is correct |
3 |
Correct |
474 ms |
8412 KB |
Output is correct |
4 |
Correct |
242 ms |
6884 KB |
Output is correct |
5 |
Correct |
138 ms |
4452 KB |
Output is correct |
6 |
Correct |
253 ms |
7956 KB |
Output is correct |
7 |
Correct |
299 ms |
8028 KB |
Output is correct |
8 |
Correct |
189 ms |
4720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
2 ms |
512 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
430 ms |
7516 KB |
Output is correct |
28 |
Correct |
433 ms |
7644 KB |
Output is correct |
29 |
Correct |
427 ms |
7644 KB |
Output is correct |
30 |
Correct |
433 ms |
7644 KB |
Output is correct |
31 |
Correct |
232 ms |
6364 KB |
Output is correct |
32 |
Correct |
106 ms |
3432 KB |
Output is correct |
33 |
Correct |
120 ms |
3436 KB |
Output is correct |
34 |
Correct |
108 ms |
3432 KB |
Output is correct |
35 |
Correct |
59 ms |
2540 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
60 ms |
2284 KB |
Output is correct |
38 |
Correct |
99 ms |
3016 KB |
Output is correct |
39 |
Correct |
221 ms |
4452 KB |
Output is correct |
40 |
Correct |
221 ms |
4596 KB |
Output is correct |
41 |
Correct |
474 ms |
8412 KB |
Output is correct |
42 |
Correct |
242 ms |
6884 KB |
Output is correct |
43 |
Correct |
138 ms |
4452 KB |
Output is correct |
44 |
Correct |
253 ms |
7956 KB |
Output is correct |
45 |
Correct |
299 ms |
8028 KB |
Output is correct |
46 |
Correct |
189 ms |
4720 KB |
Output is correct |
47 |
Correct |
230 ms |
6376 KB |
Output is correct |
48 |
Correct |
224 ms |
6372 KB |
Output is correct |
49 |
Correct |
505 ms |
12368 KB |
Output is correct |
50 |
Correct |
243 ms |
8796 KB |
Output is correct |
51 |
Correct |
186 ms |
5604 KB |
Output is correct |
52 |
Correct |
275 ms |
7644 KB |
Output is correct |
53 |
Correct |
269 ms |
8420 KB |
Output is correct |
54 |
Correct |
313 ms |
8412 KB |
Output is correct |
55 |
Correct |
398 ms |
10716 KB |
Output is correct |