#K83037. Minimizing Tree Removals to Satisfy Height Constraints
Minimizing Tree Removals to Satisfy Height Constraints
Minimizing Tree Removals to Satisfy Height Constraints
Given a row of trees with varying heights, your task is to remove the minimum number of trees so that the height difference between any two adjacent trees in the remaining sequence does not exceed a given threshold ( k ). In other words, for the remaining trees with heights ( h_1, h_2, \dots, h_m ), the condition (|h_i - h_{i+1}| \leq k) must hold for all (1 \leq i < m).
The trees are arranged in a fixed order and may only be removed (not rearranged). Determine the minimum number of removals required to meet the condition.
inputFormat
The input is read from standard input and has the following format:
( n ) — an integer representing the number of trees.
( h_1, h_2, \dots, h_n ) — a sequence of ( n ) space-separated integers representing the heights of the trees.
( k ) — an integer representing the maximum allowed height difference between any two adjacent trees.
Example:
6
4 10 6 7 8 11
2
outputFormat
Output a single integer to standard output representing the minimum number of trees that must be removed so that for every two adjacent trees in the remaining sequence, the absolute difference in their heights does not exceed ( k ).## sample
6
4 10 6 7 8 11
2
2