#include<bits/stdc++.h>
using namespace std;
#define foru(i,a,b) for(int i=(a); i<=(b); ++i)
#define ford(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i,a) for(int i=0; i<(a); ++i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define bit(s,i) (((s)>>(i))&1)
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
#define _ << " " <<
template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;}
template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;}
const int N=3e5+5;
int n,d,a[N],na[N],dp[N],r[N];
int L[N],R[N];
ii sorted[N];
vi can[N];
int par[N];
int find(int x){
return par[x]==0 ? x : par[x]=find(par[x]);
}
void unite(int x, int y){
x=find(x); y=find(y);
if(x!=y){
if(x>y) swap(x,y);
par[x]=y;
}
}
#define lc id<<1
#define rc id<<1|1
int st[N<<2];
void upd(int x, int val){
int id=1,l=1,r=n;
while(true){
if(l==r) break;
int mid=(l+r)>>1;
if(x<=mid) id=lc,r=mid;
else id=rc,l=mid+1;
}
st[id]=val;
while(id>1){
id/=2;
st[id]=max(st[lc],st[rc]);
}
}
int get(int u, int v, int id=1, int l=1, int r=n){
if(u>r||v<l) return 0;
if(u<=l&&r<=v) return st[id];
int mid=(l+r)>>1;
return max(get(u,v,lc,l,mid),get(u,v,rc,mid+1,r));
}
void solve(){
cin>>n>>d;
foru(i,1,n) cin>>a[i], sorted[i]={a[i],i};
sort(sorted+1, sorted+1+n);
foru(i,1,n) na[sorted[i].se]=i;
foru(i,1,n){
L[i]=i-1;
while(L[i]>0 && na[L[i]]>na[i]) L[i]=L[L[i]];
}
ford(i,n,1){
R[i]=i+1;
while(R[i]<=n && na[R[i]]>na[i]) R[i]=R[R[i]];
}
foru(i,1,n){
int j=i;
while(j<=n && sorted[j].fi==sorted[i].fi)++j;
--j;
foru(k,i,j){
int pos=sorted[k].se;
if(L[pos]!=0 && pos-L[pos]<=d) unite(L[pos],pos);
if(R[pos]!=n+1 && R[pos]-pos<=d) unite(pos,R[pos]);
}
foru(k,i,j){
int pos=sorted[k].se;
r[pos]=find(pos);
}
i=j;
}
foru(i,1,n){
can[r[i]].eb(i);
}
int j=1;
foru(i,1,n){
while(i-j>d){
for(int k:can[j]) upd(na[k],0);
++j;
}
int tmp=lower_bound(sorted+1, sorted+1+n, ii(a[i],0))-sorted-1;
dp[i]=get(1,tmp)+1;
upd(na[i],dp[i]);
}
cout<<*max_element(dp+1,dp+1+n);
}
int32_t main(){
#define task "test"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
int tc=1; //cin>>tc;
foru(i,1,tc){
solve();
}
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |