#pragma GCC optimize ("O2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define task "task"
#define task "task"
#define prll pair<ll,ll>
#define pb push_back
#define ld long double
#define int ll
const ll MIN=-1e18,MAX=1e18,MOD=1e9+7;
map<int,int> danh;
int tree[800005],a[200005],f[200005],f2[200005];
void update(int node,int l,int r,int u,int v)
{
if(l>u or r<u) return;
if(l==r)
{
tree[node]=max(tree[node],v);
return;
}
int m=(l+r)/2;
update(node*2,l,m,u,v);
update(node*2+1,m+1,r,u,v);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
int get(int node,int l,int r,int u,int v)
{
if(l>v or r<u) return 0;
if(l>=u and r<=v) return tree[node];
int m=(l+r)/2;
return max(get(node*2,l,m,u,v),get(node*2+1,m+1,r,u,v));
}
vector<int> lst;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
lst.push_back(a[i]);
}
sort(lst.begin(),lst.end());
int d=0,ans=0;
for(auto i:lst)
{
if(!danh[i])
{
danh[i]=++d;
}
}
for(int i=1;i<=n;i++)
{
f[i]=get(1,1,n,1,danh[a[i]]-1)+1;
update(1,1,n,danh[a[i]],f[i]);
ans=max(ans,f[i]);
// cout<<f[i]<<" ";
}
// cout<<endl;
for(int i=1;i<=4*n;i++) tree[i]=0;
for(int i=n;i>=1;i--)
{
f2[i]=get(1,1,n,danh[a[i]]+1,n)+1;
update(1,1,n,danh[a[i]],f2[i]);
// cout<<f2[i]<<" ";
}
// cout<<endl;
for(int i=1;i<=4*n;i++) tree[i]=0;
for(int i=1;i<=n;i++)
{
int idx=lower_bound(lst.begin(),lst.end(),a[i]+k)-lst.begin()-1;
ans=max(ans,get(1,1,n,1,danh[lst[idx]])+f2[i]);
// cout<<get(1,1,n,1,danh[lst[idx]])<<" ";
update(1,1,n,danh[a[i]],f[i]);
}
// cout<<endl;
for(int i=1;i<=4*n;i++) tree[i]=0;
for(int i=n;i>=1;i--)
{
int idx=lower_bound(lst.begin(),lst.end(),a[i]-k+1)-lst.begin();
// cout<<a[i]<<" "<<idx<<endl;
// cout<<danh[lst[idx]]<<endl;
if(danh[lst[idx]]!=0) ans=max(ans,get(1,1,n,danh[lst[idx]],n)+f[i]);
// cout<<ans<<" ";
update(1,1,n,danh[a[i]],f2[i]);
}
cout<<ans;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
989 ms |
26164 KB |
Output is correct |
2 |
Correct |
1014 ms |
27284 KB |
Output is correct |
3 |
Correct |
1028 ms |
27168 KB |
Output is correct |
4 |
Correct |
1004 ms |
27268 KB |
Output is correct |
5 |
Correct |
346 ms |
20192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
6988 KB |
Output is correct |
2 |
Correct |
155 ms |
6988 KB |
Output is correct |
3 |
Correct |
156 ms |
6988 KB |
Output is correct |
4 |
Correct |
74 ms |
5256 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
81 ms |
5192 KB |
Output is correct |
7 |
Correct |
133 ms |
7064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
13636 KB |
Output is correct |
2 |
Correct |
310 ms |
13740 KB |
Output is correct |
3 |
Correct |
799 ms |
26236 KB |
Output is correct |
4 |
Correct |
324 ms |
19912 KB |
Output is correct |
5 |
Correct |
188 ms |
13340 KB |
Output is correct |
6 |
Correct |
359 ms |
25076 KB |
Output is correct |
7 |
Correct |
372 ms |
24924 KB |
Output is correct |
8 |
Correct |
262 ms |
13784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
464 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
989 ms |
26164 KB |
Output is correct |
28 |
Correct |
1014 ms |
27284 KB |
Output is correct |
29 |
Correct |
1028 ms |
27168 KB |
Output is correct |
30 |
Correct |
1004 ms |
27268 KB |
Output is correct |
31 |
Correct |
346 ms |
20192 KB |
Output is correct |
32 |
Correct |
153 ms |
6988 KB |
Output is correct |
33 |
Correct |
155 ms |
6988 KB |
Output is correct |
34 |
Correct |
156 ms |
6988 KB |
Output is correct |
35 |
Correct |
74 ms |
5256 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
81 ms |
5192 KB |
Output is correct |
38 |
Correct |
133 ms |
7064 KB |
Output is correct |
39 |
Correct |
294 ms |
13636 KB |
Output is correct |
40 |
Correct |
310 ms |
13740 KB |
Output is correct |
41 |
Correct |
799 ms |
26236 KB |
Output is correct |
42 |
Correct |
324 ms |
19912 KB |
Output is correct |
43 |
Correct |
188 ms |
13340 KB |
Output is correct |
44 |
Correct |
359 ms |
25076 KB |
Output is correct |
45 |
Correct |
372 ms |
24924 KB |
Output is correct |
46 |
Correct |
262 ms |
13784 KB |
Output is correct |
47 |
Correct |
357 ms |
13756 KB |
Output is correct |
48 |
Correct |
370 ms |
13828 KB |
Output is correct |
49 |
Correct |
1002 ms |
27256 KB |
Output is correct |
50 |
Correct |
341 ms |
20232 KB |
Output is correct |
51 |
Correct |
297 ms |
16744 KB |
Output is correct |
52 |
Correct |
472 ms |
26524 KB |
Output is correct |
53 |
Correct |
374 ms |
26548 KB |
Output is correct |
54 |
Correct |
372 ms |
27304 KB |
Output is correct |
55 |
Correct |
610 ms |
27328 KB |
Output is correct |