#C9828. Minimum Operations to Achieve Maximum Non-Decreasing Sequence
Minimum Operations to Achieve Maximum Non-Decreasing Sequence
Minimum Operations to Achieve Maximum Non-Decreasing Sequence
You are given a sequence of n integers. Your task is to determine the minimum number of operations required to transform this sequence into a non-decreasing sequence. In one operation, you can swap two consecutive elements. A sequence \(a_1, a_2, \dots, a_n\) is non-decreasing if \(a_i \le a_{i+1}\) for every \(1 \le i < n\).
Example: For the sequence [3, 1, 4, 1, 5] with n = 5, the minimum number of operations needed is 2.
Your solution must read input from stdin
and write the result to stdout
.
inputFormat
The input consists of two lines:
- The first line contains an integer n, the number of elements in the sequence.
- The second line contains n space-separated integers representing the sequence.
outputFormat
Output a single integer to stdout
which represents the minimum number of adjacent swap operations required to transform the sequence into a non-decreasing sequence.
5
3 1 4 1 5
2
</p>