#K32987. Minimum Operations to Make Sequence Non-Decreasing

    ID: 24987 Type: Default 1000ms 256MiB

Minimum Operations to Make Sequence Non-Decreasing

Minimum Operations to Make Sequence Non-Decreasing

You are given a sequence of integers of length n. Your task is to determine the minimum number of operations required to transform the sequence into a non-decreasing sequence. A sequence is considered non-decreasing if it satisfies the condition \(a_i \le a_{i+1}\) for all valid indices \(i\) (i.e. for all \(1 \le i < n\)).

In each operation, you can choose an index where the sequence decreases (i.e. where \(a_i > a_{i+1}\)) and perform an operation (conceptually, you modify the value) that fixes that violation. The minimum number of operations required is equal to the number of times the sequence violates the non-decreasing condition.

Example 1: For the sequence [3, 1, 4, 1, 5], the violations occur between 3 and 1, and between 4 and 1. Hence, the answer is 2.

Example 2: For the sequence [9, 8, 7, 6, 5, 4], there are 5 violations, so the answer is 5.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains an integer n representing the number of elements in the sequence.
  • The second line contains n space-separated integers representing the sequence.

outputFormat

Output a single integer — the minimum number of operations required to make the sequence non-decreasing. The output should be written to standard output (stdout).

## sample
5
3 1 4 1 5
2