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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.io.*;
import java.util.*;

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        int n=Integer.parseInt(s[0]),k=Integer.parseInt(s[1]),N=300000+1;
        st=new int[N*4];
        int a[]=new int[N],b,i;
        for(i=0;i<n;i++)
        {
            b=Integer.parseInt(bu.readLine());
            int l=Math.max(b-k,0),r=Math.min(b+k,N-1);
            int m=max(0,N-1,l,r,0)+1;
            updateValue(a,0,N-1,b,m,0);
        }
        int ans=0;
        for(i=0;i<N;i++)
        ans=Math.max(ans,a[i]);
        System.out.print(ans);
    }

    static int st[];
    static int max(int ss,int se,int l,int r,int i)
    {
        if(l<=ss && r>=se) return st[i];
        if(se<l || ss>r) return -1;
        int mid=ss+(se-ss)/2;
        return Math.max(max(ss,mid,l,r,2*i+1),max(mid+1,se,l,r,2*i+2));
    }

    static void updateValue(int a[],int ss,int se,int i,int v,int n)
    {
        if(ss==se)
        {
            a[i]=v;
            st[n]=v;
        }
        else
        {
            int mid=ss+(se-ss)/2;
            if(i>=ss && i<=mid) updateValue(a,ss,mid,i,v,2*n+1);
            else updateValue(a,mid+1,se,i,v,2*n+2);
            st[n]=Math.max(st[2*n+1],st[2*n+2]);
        }
    }
}