E - Smooth Subsequence Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 525525

問題文

長さ NN の数列 A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) が与えられます。

AA の部分列であって、隣接する 22 項の差の絶対値が DD 以下であるようなものの長さの最大値を求めてください。

ただし、数列 AA の部分列とは、AA の要素を 00 個以上選んで削除し、残った要素を元の順序を保って並べた数列のことを指します。

制約

  • 1N5×1051 \leq N \leq 5 \times 10^5
  • 0D5×1050 \leq D \leq 5 \times 10^5
  • 1Ai5×1051 \leq A_i \leq 5 \times 10^5
  • 入力される数値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN DD
A1A_1 A2A_2 \ldots ANA_N

出力

答えを出力せよ。


入力例 1Copy

Copy
4 2
3 5 1 2

出力例 1Copy

Copy
3

AA の部分列 (3,1,2)(3, 1, 2) は隣接する 22 項の差の絶対値が 22 以下です。


入力例 2Copy

Copy
5 10
10 20 100 110 120

出力例 2Copy

Copy
3

入力例 3Copy

Copy
11 7
21 10 3 19 28 12 11 3 3 15 16

出力例 3Copy

Copy
6

Score: 525525 points

Problem Statement

You are given a sequence A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) of length NN.

Find the maximum length of a subsequence of AA such that the absolute difference between any two adjacent terms is at most DD.

A subsequence of a sequence AA is a sequence that can be obtained by deleting zero or more elements from AA and arranging the remaining elements in their original order.

Constraints

  • 1N5×1051 \leq N \leq 5 \times 10^5
  • 0D5×1050 \leq D \leq 5 \times 10^5
  • 1Ai5×1051 \leq A_i \leq 5 \times 10^5
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN DD
A1A_1 A2A_2 \ldots ANA_N

Output

Print the answer.


Sample Input 1Copy

Copy
4 2
3 5 1 2

Sample Output 1Copy

Copy
3

The subsequence (3,1,2)(3, 1, 2) of AA has absolute differences of at most 22 between adjacent terms.


Sample Input 2Copy

Copy
5 10
10 20 100 110 120

Sample Output 2Copy

Copy
3

Sample Input 3Copy

Copy
11 7
21 10 3 19 28 12 11 3 3 15 16

Sample Output 3Copy

Copy
6


2025-04-27 (Sun)
04:42:34 +00:00