Problem:
Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

10 3
1
5
4
3
8
6
9
7
2
4

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<iomanip>


using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;


#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(),(x).end()


int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

struct Segtree{
    int N;
    vector<int> data;
    const int DEFAULT = 0;

    Segtree(int n){
        N=1;
        while(N <= n) N*=2;
        data.resize(2*N-1);
        for(int i=0;i<2*N-1;i++){
            data[i] = DEFAULT; 
            //maxの時は-1とか-INFとか、sumなら0
        }
    }

    void update(int k, int a){
        k+=N-1;//kは0-indexed
        data[k] = a;

        while(k > 0){
            k = (k-1)/2;
            data[k] = max(data[2*k+1],data[2*k+2]); 
            //min,max,sumなどで可変
        }
    }

    int query(int a,int b){return query(a, b, 0, 0, N);}
    
    int query(int a, int b, int k, int l, int r){
        // [l,r)より、r未満になるように値を入力する必要がある
        if(r <= a || b <= l) return DEFAULT; //初期化値
        if(a <= l && r <= b) return data[k];
        int vl = query(a, b, 2*k+1, l, (l+r)/2);
        int vr = query(a, b, 2*k+2, (l+r)/2, r);
        return max(vl,vr);//minじゃなくても
    }

    int a(int k){
        return data[N+k-1]; //kは0-indexed
    }
};

int main(){
    int N,K;
    cin>>N>>K;
    Segtree S(300010);
    //S.update(1,5);
    //cout<<S.query(0,0)<<S.query(1,1)<<S.query(1,2);
    rep(i,0,N){
        int a;
        cin>>a;
        int now = S.query(max(a-K,0),min(a+K+1,300001));
        S.update(a,now+1);
    }

    cout<<S.query(0,300002);
    
    return 0;
}