This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n,x;
const int N=200009;
map<long long,long long>m;
int tree[11*N],tree1[11*N],a[N];
void update(int ind,int st,int end,int uind,int uval)
{
if (st==end)
{
tree[ind]=max(tree[ind],uval);
return ;
}
int m=(st+end)/2;
if (uind>m)update(ind*2+2,m+1,end,uind,uval);
else update(ind*2+1,st,m,uind,uval);
tree[ind]=max(tree[ind*2+1],tree[ind*2+2]);
}
void update1(int ind,int st,int end,int uind,int uval)
{
if (st==end)
{
tree1[ind]=max(tree1[ind],uval);
return ;
}
int m=(st+end)/2;
if (uind>m)update1(ind*2+2,m+1,end,uind,uval);
else update1(ind*2+1,st,m,uind,uval);
tree1[ind]=max(tree1[ind*2+1],tree1[ind*2+2]);
}
int query(int ind,int st,int end,int l,int r)
{
if (l<=st&&end<=r)return tree[ind];
if (l>end||r<st)return 0;
int m=(st+end )/2;
return max(query(ind*2+1,st,m,l,r),query(ind*2+2,m+1,end,l,r));
}
int query1(int ind,int st,int end,int l,int r)
{
if (l<=st&&end<=r)return tree1[ind];
if (l>end||r<st)return 0;
int m=(st+end )/2;
return max(query1(ind*2+1,st,m,l,r),query1(ind*2+2,m+1,end,l,r));
}
int main()
{ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>x;
for(int i=0;i<n;i++){
cin>>a[i];
m[a[i]]++;
m[a[i]-x]++;
m[a[i]+x]++;
}
int re=0;
for(auto it:m)
{
m[it.first]=re++;
}
int mx=0;
for(int i=0;i<n;i++)
{
int xx=query(0,0,re,0,m[a[i]]-1);
update(0,0,re,m[a[i]],xx+1);
int y=query(0,0,re,0,m[a[i]+x]-1);
y=max(query1(0,0,re,0,m[a[i]]-1),y);
update1(0,0,re,m[a[i]],y+1);
mx=max(mx,y+1);
mx=max(mx,xx+1);
}
cout<<max(query(0,0,re,0,re),query1(0,0,re,0,re));
}
# | 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... |