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)

/* by Natsu Kinmoe */
#include <bits/stdc++.h>
using namespace std;
#define SZ(x)               ((int)(x).size())
#define all(x)              (x).begin(), (x).end()
#define loop(i, n)          for(int i = 0; i < (n); i++)
#define cont(i, n)          for(int i = 1; i <= (n); i++)
#define circ(i, a, b)       for(int i = (a); i <= (b); i++)
#define range(i, a, b, c)   for(int i = (a); ((c) > 0 ? i <= (b) : i >= (b)); i += (c))
#define foreach(it, v)      for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define y0          y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0
#define y1          y1II11II11III11I1III11II111IIII1II1I1
#define pub         push_back
#define pob         pop_back
#define mak         make_pair
typedef long long   ll;
typedef long double lf;
const int   Inf = 0x3f3f3f3f;
const ll    INF = 0x3f3f3f3f3f3f3f3fll;
/* Source code starts here */

int n, k;
const int maxn = 1 << 19;

struct Segtree {
	int dt[maxn << 1];
	void inline add(int a, int x) {
		for(a += maxn; a; a >>= 1) dt[a] = max(dt[a], x);
	}
	int inline qmax(int l, int r) {
		int res = 0;
		for(l += maxn, r += maxn + 1; l < r; l >>= 1, r >>= 1) {
			if(l & 1) res = max(res, dt[l++]);
			if(r & 1) res = max(res, dt[--r]);
		}
		return res;
	}
} seg;

int a[maxn];

int main() {
	scanf("%d%d", &n, &k);
	cont(i, n) scanf("%d", a + i);
	cont(i, n) {
		int mx = seg.qmax(max(0, a[i] - k), min(maxn - 1, a[i] + k)) + 1;
		seg.add(a[i], mx);
	}
	printf("%d\n", seg.qmax(0, maxn - 1));
	return 0;
}