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;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
struct segtree{
ll n;
vector<ll>d;
segtree(ll n):n(n){
d.resize(4*n);
build(1,1,n);
}
void build(ll node,ll l,ll r){
if(l==r){
d[node]=0;
return;
}
ll mid=(l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid+1,r);
d[node]=0;
}
ll query(ll node,ll l,ll r,ll L,ll R){
if(R<L||r<L||R<l) return 0ll;
if(L<=l&&r<=R){
return d[node];
}
ll mid=(l+r)/2;
return max(query(node*2,l,mid,L,R),query(node*2+1,mid+1,r,L,R));
}
void update(ll node,ll l,ll r,ll pos,ll val){
if(pos<l||r<pos) return;
if(l==r){
d[node]=val;
return;
}
ll mid=(l+r)/2;
update(node*2,l,mid,pos,val);
update(node*2+1,mid+1,r,pos,val);
d[node]=max(d[node*2],d[node*2+1]);
}
};
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,x;
cin>>n>>x;
ll t[n+5];
for(ll i=1;i<=n;i++){
cin>>t[i];
}
ll pre[n+5],ans=0;
vector<ll>v;
for(ll i=1;i<=n;i++){
if(v.empty()||v.back()<t[i]){
v.pb(t[i]);
pre[i]=v.size();
}
else{
ll x=lower_bound(all(v),t[i])-v.begin();
v[x]=t[i];
pre[i]=x+1;
}
}
ans=v.size();
v.clear();
ll suf[n+5];
suf[n+1]=0;
for(ll i=n;i>=1;i--){
if(v.empty()||v.back()<-t[i]){
v.pb(-t[i]);
suf[i]=v.size();
}
else{
ll x=lower_bound(all(v),-t[i])-v.begin();
v[x]=-t[i];
suf[i]=x+1;
}
}
segtree sg(n);
pair<ll,ll>p[n+5];
for(ll i=1;i<=n;i++){
p[i].ff=t[i];
p[i].ss=i;
}
sort(p+1,p+n+1);
p[n+1].ss=0;
ll l=n+1;
for(ll i=n;i>=1;i--){
while(l>1&&p[l-1].ff>p[i].ff-x){
l--;
sg.update(1,1,n,p[l].ss,suf[p[l].ss]);
}
ans=max(ans,pre[p[i].ss]+sg.query(1,1,n,p[i].ss+1,n));
}
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... |