#C8268. Robotic Assistant - Minimum Adjacent Swaps
Robotic Assistant - Minimum Adjacent Swaps
Robotic Assistant - Minimum Adjacent Swaps
A robotic assistant is assigned the task of arranging a row of books by their thickness. The assistant can only swap two adjacent books at a time. The goal is to determine the minimum number of adjacent swaps required to sort the books in non-decreasing order.
For instance, given an array representing the thickness values of the books, such as [4, 3, 1, 2, 5]
, the assistant must compute the minimal swaps needed to arrange them as [1, 2, 3, 4, 5]
. In this example, the correct answer is 5 swaps.
inputFormat
The input is read from stdin
and consists of two lines:
- The first line contains a single integer N indicating the number of books.
- The second line contains N space-separated integers representing the thickness of each book.
outputFormat
The output should be written to stdout
and is a single integer representing the minimum number of adjacent swaps needed to sort the array into non-decreasing order.
5
4 3 1 2 5
5