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>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5+10;
const ll inf=0;
const ll mod=1e9+7;
ll n,a[maxN];
ll st[4*maxN];
void build()
{
fill(st,st+4*maxN,-inf);
}
void update(ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(l==r)
{
st[id]=max(st[id],val);
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,val,id*2,l,mid);
else update(pos,val,id*2+1,mid+1,r);
st[id]=max(st[id*2],st[id*2+1]);
}
ll get(ll i,ll j,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i) return -inf;
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return max(get(i,j,id*2,l,mid),get(i,j,id*2+1,mid+1,r));
}
ll x,dp1[maxN],dp2[maxN];
vector<ll>vec;
void solve()
{
cin >> n >> x;
vec.pb(-1);
for(int i=1;i<=n;i++) cin >>a[i],vec.pb(a[i]);
sort(vec.begin(),vec.end());
for(int i=1;i<=n;i++)
{
ll p=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin()-1;
dp1[i]=max(1ll,get(1,p)+1);
p++;
update(p,dp1[i]);
}
build();
for(int i=n;i>=1;i--)
{
ll p=upper_bound(vec.begin(),vec.end(),a[i])-vec.begin();
dp2[i]=max(1ll,get(p,n)+1);
p--;
update(p,dp2[i]);
}
build();
ll ans=dp1[n];
for(int i=1;i<=n;i++)
{
ll p=lower_bound(vec.begin(),vec.end(),a[i]+x)-vec.begin()-1;
ans=max(ans,dp2[i]+get(1,p));
p=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin();
update(p,dp1[i]);
}
build();
for(int i=n;i>=1;i--)
{
ll p=upper_bound(vec.begin(),vec.end(),a[i]-x)-vec.begin();
ans=max(ans,dp1[i]+get(p,n));
p=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin();
update(p,dp2[i]);
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
glo.cpp: In function 'void update(ll, ll, ll, ll, ll)':
glo.cpp:26:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | ll mid=l+r>>1;
| ~^~
glo.cpp: In function 'll get(ll, ll, ll, ll, ll)':
glo.cpp:35:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | ll mid=l+r>>1;
| ~^~
# | 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... |