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 Rust (1.42.0) (judge verdict: Wrong Answer)

#![allow(non_snake_case)]
use std::io::*;
use std::str::FromStr;
use std::cmp::*;

#[allow(dead_code)]
fn read<T : FromStr>() -> T {
  let token: String = stdin().lock()
    .bytes()
    .map(|c| c.expect("failed reading char") as char)
    .skip_while(|c| c.is_whitespace())
    .take_while(|c| !c.is_whitespace())
    .collect();
  token.parse().ok().expect("failed parse")
}

#[allow(dead_code)]
fn read2<T : std::str::FromStr>() -> (T, T) {
  (read::<T>(), read::<T>())
}

#[allow(dead_code)]
fn read3<T : std::str::FromStr>() -> (T, T, T) {
  (read::<T>(), read::<T>(), read::<T>())
}

#[allow(dead_code)]
fn reads<T : std::str::FromStr>() -> Vec<T> {
  let mut line = String::new();
  stdin().read_line(&mut line).ok();
  line.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

struct MaxSegTree {
  n: usize,
  data: Vec<i64>,
}

impl MaxSegTree {
  fn new(data_: &[i64]) -> Self {
    let m = data_.len();
    let mut n = 1;
    while n < m { n *= 2; }

    let mut data = vec![std::i64::MIN; 2*n];

    for i in 0..m {
      data[i + n - 1] = data_[i];
    }

    for ri in 0..n-2 {
      let i = n - 2 - ri; // [n-2 .. 0]
      data[i] = max(data[i*2+1], data[i*2+2]);
    }

    Self {
      n,
      data,
    }
  }

  fn update(&mut self, i: usize, val: i64) {
    let mut i = self.n - 1 + i;
    self.data[i] = val;
    while i > 0 {
      i = (i - 1) / 2;
      self.data[i] = max(self.data[i*2+1], self.data[i*2+2]);
    }
  }

  fn query(&self, l: usize, r: usize) -> i64 {
    let mut l = l + self.n - 1;
    let mut r = r + self.n - 1;

    let mut res = std::i64::MIN;
    while l < r {
      if (l & 1) == 0 {
        res = max(res, self.data[l]);
      }
      if (r & 1) == 0 {
        res = max(res, self.data[r]);
      }
      l = l / 2;
      r = (r - 1) / 2;
    }

    res
  }
}

fn main() {
  let n: usize = read();
  let k: usize = read();
  let mut xs: Vec<usize> = Vec::with_capacity(n);
  for _ in 0..n {
    let x = read();
    xs.push(x);
  }

  const LIMIT: usize = 300000 + 10;
  let mut seg = MaxSegTree::new(&vec![0; LIMIT]);
  let mut res = 1;
  for x in xs {
    let l = if x >= k { x - k } else { 0 };
    let r = if x + k + 1 <= LIMIT { x + k + 1 } else { LIMIT };
    let head = seg.query(l, r);

    seg.update(x, head + 1);
    res = max(res, head + 1);
  }

  // let res = seg.query(0, LIMIT);
  println!("{}", res);
}