#C8133. Minimum Operations to Achieve Non-decreasing Sequence
Minimum Operations to Achieve Non-decreasing Sequence
Minimum Operations to Achieve Non-decreasing Sequence
You are given an array of n integers representing the prices of houses in a row. In one operation, you can select a contiguous segment of the array that is non-increasing (i.e. for every index i in the segment, A[i] ≥ A[i+1]) and replace all the elements in this segment with the minimum value found in that segment.
Your task is to determine the minimum number of such operations required to transform the array into a non-decreasing sequence. In other words, after performing the operations, the resulting array A should satisfy the condition:
$$A[i] \le A[i+1] \quad \text{for all} \quad 1 \le i < n. $$Example:
Input: 5 4 3 2 5 4</p>One way to achieve the transformation: Operation 1: Select subarray [4, 3, 2] and replace with 2. ( [4, 3, 2, 5, 4] ) becomes ( [2, 2, 2, 5, 4] ). Operation 2: Select subarray [5, 4] and replace with 4. ( [2, 2, 2, 5, 4] ) becomes ( [2, 2, 2, 4, 4] ).
Thus, the minimum number of operations is 2.
inputFormat
The first line contains a single integer n (1 ≤ n ≤ 105), the number of houses. The second line contains n space-separated integers representing the prices of the houses.
outputFormat
Output a single integer, the minimum number of operations required to make the array non-decreasing.
## sample5
4 3 2 5 4
2
</p>