#include <bits/stdc++.h>
using namespace std;
const int N=2e6+5;
const long long MAXN=1e9+7;
int n;
int d;
vector<int>a(N);
vector<int>F(N);
struct implicit{
int idxx=1;
int t[4*N];
int ll[4*N];
int rr[4*N];
void update(int node,int tl,int tr,int index,int value){
if(tl==tr){
t[node]=value;
return;
}
int tm=(tl+tr)/2;
if(index<=tm){
if(!ll[node])ll[node]=++idxx;
update(ll[node],tl,tm,index,value);
}
else{
if(!rr[node])rr[node]=++idxx;
update(rr[node],tm+1,tr,index,value);
}
t[node]=max((ll[node]?t[ll[node]]:0),(rr[node]?t[rr[node]]:0));
}
void update(int i,int v){update(1,1,MAXN,i,v);}
int query(int node,int tl,int tr,int l,int r){
if(tl>=l&&tr<=r)return t[node];
int tm=(tl+tr)/2;
int ans=0;
if(ll[node]&&l<=tm)ans=query(ll[node],tl,tm,l,r);
if(rr[node]&&tm<r)ans=max(ans,query(rr[node],tm+1,tr,l,r));
return ans;
}
int query(int l,int r){return query(1,1,MAXN,l,r);}
}seg;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>d;
for(int i=1;i<=n;i++)cin>>a[i];
F=a;
vector<int>kompres;
for(int i=1;i<=n;i++)kompres.push_back(a[i]);
sort(kompres.begin(),kompres.end());
for(int i=1;i<=n;i++)a[i]=lower_bound(kompres.begin(),kompres.end(),a[i])-kompres.begin()+1;
int lis[n+1];
for(int i=0;i<=n;i++)lis[i]=1;
for(int i=1;i<=n;i++){
lis[i]=seg.query(0,a[i]-1)+1;
seg.update(a[i],lis[i]);
}
for(int i=1;i<=n;i++)seg.update(a[i],0);
int suflis[n+1];
for(int i=0;i<=n;i++)suflis[i]=1;
for(int i=n;i>=1;i--){
suflis[i]=seg.query(a[i]+1,n)+1;
seg.update(a[i],suflis[i]);
}
int ans=0;
for(int i=1;i<=n;i++)ans=max(ans,suflis[i]);
for(int i=1;i<=n;i++)seg.update(a[i],0);
/*
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(abs(F[i]-F[j])<d){
ans=max(ans,lis[i]+suflis[j]);
}
}
}*/
if(!d){
cout<<ans;
return 0;
}
d--;
for(int i=n;i>=1;i--){
ans=max(ans,lis[i]+seg.query(F[i]-d,F[i]));
ans=max(ans,lis[i]+seg.query(F[i],F[i]+d));
seg.update(F[i],suflis[i]);
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15964 KB |
Output is correct |
2 |
Correct |
4 ms |
16064 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
4 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
16004 KB |
Output is correct |
8 |
Correct |
4 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
4 ms |
15964 KB |
Output is correct |
11 |
Correct |
4 ms |
15964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15964 KB |
Output is correct |
2 |
Correct |
4 ms |
16064 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
4 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
16004 KB |
Output is correct |
8 |
Correct |
4 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
4 ms |
15964 KB |
Output is correct |
11 |
Correct |
4 ms |
15964 KB |
Output is correct |
12 |
Correct |
4 ms |
15960 KB |
Output is correct |
13 |
Correct |
4 ms |
15964 KB |
Output is correct |
14 |
Correct |
4 ms |
15964 KB |
Output is correct |
15 |
Correct |
4 ms |
15964 KB |
Output is correct |
16 |
Correct |
4 ms |
15964 KB |
Output is correct |
17 |
Correct |
4 ms |
15960 KB |
Output is correct |
18 |
Correct |
5 ms |
15964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15964 KB |
Output is correct |
2 |
Correct |
4 ms |
16064 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
4 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
16004 KB |
Output is correct |
8 |
Correct |
4 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
4 ms |
15964 KB |
Output is correct |
11 |
Correct |
4 ms |
15964 KB |
Output is correct |
12 |
Correct |
4 ms |
15960 KB |
Output is correct |
13 |
Correct |
4 ms |
15964 KB |
Output is correct |
14 |
Correct |
4 ms |
15964 KB |
Output is correct |
15 |
Correct |
4 ms |
15964 KB |
Output is correct |
16 |
Correct |
4 ms |
15964 KB |
Output is correct |
17 |
Correct |
4 ms |
15960 KB |
Output is correct |
18 |
Correct |
5 ms |
15964 KB |
Output is correct |
19 |
Correct |
6 ms |
16220 KB |
Output is correct |
20 |
Correct |
7 ms |
16476 KB |
Output is correct |
21 |
Correct |
6 ms |
16220 KB |
Output is correct |
22 |
Correct |
5 ms |
15964 KB |
Output is correct |
23 |
Correct |
6 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
15964 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
325 ms |
23712 KB |
Output is correct |
2 |
Correct |
326 ms |
25804 KB |
Output is correct |
3 |
Correct |
330 ms |
25804 KB |
Output is correct |
4 |
Correct |
323 ms |
25868 KB |
Output is correct |
5 |
Correct |
204 ms |
23320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
27812 KB |
Output is correct |
2 |
Correct |
105 ms |
27776 KB |
Output is correct |
3 |
Correct |
105 ms |
27880 KB |
Output is correct |
4 |
Correct |
73 ms |
22484 KB |
Output is correct |
5 |
Correct |
4 ms |
15960 KB |
Output is correct |
6 |
Correct |
80 ms |
19608 KB |
Output is correct |
7 |
Correct |
90 ms |
26052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
38612 KB |
Output is correct |
2 |
Correct |
228 ms |
39684 KB |
Output is correct |
3 |
Correct |
483 ms |
57024 KB |
Output is correct |
4 |
Correct |
302 ms |
40972 KB |
Output is correct |
5 |
Correct |
155 ms |
21712 KB |
Output is correct |
6 |
Correct |
272 ms |
24640 KB |
Output is correct |
7 |
Correct |
272 ms |
30412 KB |
Output is correct |
8 |
Correct |
193 ms |
34228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
15964 KB |
Output is correct |
2 |
Correct |
4 ms |
16064 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
4 ms |
15964 KB |
Output is correct |
5 |
Correct |
4 ms |
15964 KB |
Output is correct |
6 |
Correct |
4 ms |
15964 KB |
Output is correct |
7 |
Correct |
4 ms |
16004 KB |
Output is correct |
8 |
Correct |
4 ms |
15964 KB |
Output is correct |
9 |
Correct |
3 ms |
15964 KB |
Output is correct |
10 |
Correct |
4 ms |
15964 KB |
Output is correct |
11 |
Correct |
4 ms |
15964 KB |
Output is correct |
12 |
Correct |
4 ms |
15960 KB |
Output is correct |
13 |
Correct |
4 ms |
15964 KB |
Output is correct |
14 |
Correct |
4 ms |
15964 KB |
Output is correct |
15 |
Correct |
4 ms |
15964 KB |
Output is correct |
16 |
Correct |
4 ms |
15964 KB |
Output is correct |
17 |
Correct |
4 ms |
15960 KB |
Output is correct |
18 |
Correct |
5 ms |
15964 KB |
Output is correct |
19 |
Correct |
6 ms |
16220 KB |
Output is correct |
20 |
Correct |
7 ms |
16476 KB |
Output is correct |
21 |
Correct |
6 ms |
16220 KB |
Output is correct |
22 |
Correct |
5 ms |
15964 KB |
Output is correct |
23 |
Correct |
6 ms |
16220 KB |
Output is correct |
24 |
Correct |
5 ms |
16220 KB |
Output is correct |
25 |
Correct |
5 ms |
15964 KB |
Output is correct |
26 |
Correct |
5 ms |
15964 KB |
Output is correct |
27 |
Correct |
325 ms |
23712 KB |
Output is correct |
28 |
Correct |
326 ms |
25804 KB |
Output is correct |
29 |
Correct |
330 ms |
25804 KB |
Output is correct |
30 |
Correct |
323 ms |
25868 KB |
Output is correct |
31 |
Correct |
204 ms |
23320 KB |
Output is correct |
32 |
Correct |
113 ms |
27812 KB |
Output is correct |
33 |
Correct |
105 ms |
27776 KB |
Output is correct |
34 |
Correct |
105 ms |
27880 KB |
Output is correct |
35 |
Correct |
73 ms |
22484 KB |
Output is correct |
36 |
Correct |
4 ms |
15960 KB |
Output is correct |
37 |
Correct |
80 ms |
19608 KB |
Output is correct |
38 |
Correct |
90 ms |
26052 KB |
Output is correct |
39 |
Correct |
226 ms |
38612 KB |
Output is correct |
40 |
Correct |
228 ms |
39684 KB |
Output is correct |
41 |
Correct |
483 ms |
57024 KB |
Output is correct |
42 |
Correct |
302 ms |
40972 KB |
Output is correct |
43 |
Correct |
155 ms |
21712 KB |
Output is correct |
44 |
Correct |
272 ms |
24640 KB |
Output is correct |
45 |
Correct |
272 ms |
30412 KB |
Output is correct |
46 |
Correct |
193 ms |
34228 KB |
Output is correct |
47 |
Correct |
228 ms |
39628 KB |
Output is correct |
48 |
Correct |
219 ms |
39416 KB |
Output is correct |
49 |
Correct |
508 ms |
57296 KB |
Output is correct |
50 |
Correct |
281 ms |
40740 KB |
Output is correct |
51 |
Correct |
237 ms |
22988 KB |
Output is correct |
52 |
Correct |
338 ms |
25228 KB |
Output is correct |
53 |
Correct |
295 ms |
25044 KB |
Output is correct |
54 |
Correct |
298 ms |
31036 KB |
Output is correct |
55 |
Correct |
397 ms |
48964 KB |
Output is correct |