#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define double long double
#define x first
#define y second
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
using pii=pair <int,int>;
using tii=pair <pii,int>;
struct Seg{
vector <int> seg;
void init(int n){
seg.resize(4*n+10);
}
void update(int id,int tl,int tr,int pos,int val){
if (tl==tr){
seg[id]=val;
return;
}
int tm=(tl+tr)/2;
if (pos<=tm) update(2*id,tl,tm,pos,val);
else update(2*id+1,tm+1,tr,pos,val);
seg[id]=max(seg[2*id],seg[2*id+1]);
}
int query(int id,int tl,int tr,int l,int r){
if (l>r) return 0;
if (l<=tl&&tr<=r) return seg[id];
int tm=(tl+tr)/2;
int lx=query(2*id,tl,tm,l,min(r,tm));
int rx=query(2*id+1,tm+1,tr,max(l,tm+1),r);
return max(lx,rx);
}
};
void solve(){
int n,d;
cin>>n>>d;
int a[n+1];
for (int i=1; i<=n; i++) cin>>a[i];
map <int,vector <int> > mp;
for (int i=1; i<=n; i++) mp[a[i]].pb(i);
Seg seg;
seg.init(n);
for (auto i:mp){
vector <pii> vec;
int prv=0;
for (int j:i.y){
int ths=seg.query(1,1,n,max(1LL,j-d),j-1)+1;
ths=max(ths,prv);
vec.pb({j,ths});
prv=max(prv,ths);
}
for (pii j:vec) seg.update(1,1,n,j.x,j.y);
}
cout<<seg.query(1,1,n,1,n)<<'\n';
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t=1;
//cin>>t;
while (t--) solve();
}
# | 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... |