#K65697. Minimum Replacements for Non-Decreasing Sequence
Minimum Replacements for Non-Decreasing Sequence
Minimum Replacements for Non-Decreasing Sequence
Lara has a collection of n plant species with given growth rates. She wants to make the sequence of growth rates non-decreasing. To achieve this, she is allowed to replace any plant with a new species whose growth rate is fixed to a given value x. Your task is to determine the minimum number of replacements required so that the resulting sequence is non-decreasing (i.e. for every adjacent pair, the left value is less than or equal to the right value).
Note: The order of the plants is fixed. When a replacement is made, the growth rate at that position becomes exactly x. It is guaranteed that the input values are chosen such that applying the greedy left-to-right replacement (i.e. whenever an adjacent pair violates the non-decreasing order, replace the right element with x) will yield the intended result.
inputFormat
The first line contains a single integer n — the number of plant species.
The second line contains n space-separated integers representing the growth rates of the plants.
The third line contains a single integer x — the growth rate of the new species available for replacement.
outputFormat
Output a single integer — the minimum number of replacements required to transform the given sequence into a non-decreasing sequence.
## sample5
3 2 5 1 7
4
2
</p>