#C10498. Longest Non-Decreasing Subsequence Under Removal Constraint
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:
- Initialize an empty sequence
S
. - For each element
a
in the array:- If
S
is empty or the last element ofS
is less than or equal toa
, appenda
toS
. - Otherwise, if the condition
\(
|S_{last} - a| \le k
\)
holds (where \(S_{last}\) denotes the last element of
S
), skip the elementa
. - If neither condition holds, reset the sequence
S
to contain onlya
(i.e. set \(S = [a]\)).
- If
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.
## sample3
5 1
4 1 2 3 5
4 3
10 7 5 8
3 0
1 3 5
4
2
3
</p>