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 with AC Library v1.1) (judge verdict: Accepted)

// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")



#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// #define Find order_of_key
// #define at find_by_order
#define x first
#define y second
#define ll long long
#define lll __int128
#define ull unsigned long long
#define ulll unsigned __int128
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define gu getchar_unlocked()
#define pu putchar_unlocked()
#define all(x) x.begin(),x.end()
#define endl '\n'

int n=300000;
struct segtree {
	static const int maxn = 300010;
	int tree[maxn << 2]={0};
	void build(int node = 1, int l = 0, int r = n) {
		if (l == r) {tree[node] = 0; return; }
		int mid = l + r >> 1;
		build(node << 1, l, mid);
		build(node << 1 | 1, mid + 1, r);
		tree[node] = max(tree[node << 1] , tree[node << 1 | 1]);
	}
	void update(int pos, ll v, int node = 1, int l = 0, int r = n) {
		if (r < pos || l > pos) return;
		if (pos == l && pos == r) {
			tree[node] = v;
			return;
		} int mid = l + r >> 1;
		update(pos, v, node << 1, l, mid);
		update(pos, v, node << 1 | 1, mid + 1, r);
		tree[node] = max(tree[node << 1] , tree[node << 1 | 1]);
	}
	ll query(int i, int j, int node = 1, int l = 0, int r = n) {
		if (r < i || l > j) return -1;
		if (i <= l && r <= j) {
			return tree[node];
		} int mid = l + r >> 1;
		return max(query(i, j, node << 1, l, mid) ,
		           query(i, j, node << 1 | 1, mid + 1, r));
	}
}sg;

int main()
{
	sg.build();
	int nn,k;
	cin>>nn>>k;
	for(int i=1;i<=nn;i++){
		int a;
		cin>>a;
		int l=max(a-k,0);
		int r=min(a+k,300000);
		int g=sg.query(l,r);
		sg.update(a,g+1);
	}cout<<sg.query(0,n)<<endl;
	return 0;
}