#C10498. Longest Non-Decreasing Subsequence Under Removal Constraint

    ID: 39709 Type: Default 1000ms 256MiB

Longest Non-Decreasing Subsequence Under Removal Constraint

Longest Non-Decreasing Subsequence Under Removal Constraint

You are given t test cases. For each test case, you are provided an integer n, an integer k, and an array of n integers. Your task is to determine the length of a special subsequence constructed by processing the array from left to right using the following procedure:

  1. Initialize an empty sequence S.
  2. For each element a in the array:
    • If S is empty or the last element of S is less than or equal to a, append a to S.
    • Otherwise, if the condition \( |S_{last} - a| \le k \) holds (where \(S_{last}\) denotes the last element of S), skip the element a.
    • If neither condition holds, reset the sequence S to contain only a (i.e. set \(S = [a]\)).

After processing all elements, output the length of S.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains a single integer t denoting the number of test cases.
  • Each test case consists of two lines:
    • The first line of a test case contains two space-separated integers, n and k.
    • The second line contains n space-separated integers representing the array.

outputFormat

For each test case, output a single integer — the length of the subsequence generated by the above process. Each answer should be printed on a new line.

## sample
3
5 1
4 1 2 3 5
4 3
10 7 5 8
3 0
1 3 5
4

2 3

</p>