This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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(){
// #ifndef ONLINE_JUDGE
// freopen (task".inp", "r", stdin);
// freopen (task".out", "w", stdout);
// #endif
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(auto i:lst)
// {
// cout<<i<<" ";
// }
// cout<<endl;
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=upper_bound(lst.begin(),lst.end(),a[i]+k-1)-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=upper_bound(lst.begin(),lst.end(),a[i]-k+1)-lst.begin();
// cout<<idx<<endl;
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |