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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using static System.Console;

namespace abl
{
    class Program
    {
        static void Main()
        {
            var nk = ReadLine().Split(' ');
            var n = int.Parse(nk[0]);
            var k = int.Parse(nk[1]);
            var aList = new int[n];
            for (var i = 0; i < n; ++i)
            {
                aList[i] = int.Parse(ReadLine());
            }
            WriteLine(FS(k, aList));
        }
        static int FS(int k, int[] aList)
        {
            var tree = new MaxSegmentTree(300010);
            foreach (var a in aList)
            {
                var sMin = Math.Max(0, a - k);
                var sMax = Math.Min(300000, a + k);
                var len = tree.Find(sMin, sMax + 1);
                if (len < 0)
                {
                    len = 0;
                }
                tree.Update(a, len + 1);
            }
            return tree.Find(0, 300001);
        }
        class MaxSegmentTree
        {
            public int[] tree { get; private set; }
            int INF = int.MinValue;
            public MaxSegmentTree(int _n)
            {
                var x = 1;
                while (_n > x)
                {
                    x *= 2;
                }
                var n = x * 2;
                tree = new int[n];
                for (var i = 0; i < n; ++i)
                {
                    tree[i] = INF;
                }
            }
            public void Update(int index, int value)
            {
                var idx = index + tree.Length / 2;
                tree[idx] = value;
                while (idx > 1)
                {
                    var parent = idx / 2;
                    var siblings = idx + 1 - (idx % 2) * 2;
                    tree[parent] = Math.Max(tree[idx], tree[siblings]);
                    idx = parent;
                }
            }
            public int Find(int a, int b)
            {
                return Find(a, b, 1, 0, tree.Length / 2);
            }
            int Find(int a, int b, int k, int l, int r)
            {
                if (r <= a || b <= l)
                {
                    return INF;
                }
                else if (a <= l && r <= b)
                {
                    return tree[k];
                }
                else
                {
                    return Math.Max(
                        Find(a, b, k * 2, l, (l + r) / 2),
                        Find(a, b, k * 2 + 1, (l + r) / 2, r)
                    );
                }
            }
        }
    }
}