#FT0000069. Số lần đổi chỗ kề ít nhất (Minimum Adjacent Swaps)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Số lần đổi chỗ kề ít nhất (Minimum Adjacent Swaps)
Minimum Adjacent Swaps
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
In a contest to select a wise person, an integer sequence is given in arbitrary order. The only permitted operation is to swap two elements at adjacent positions.
The goal is to transform the sequence into a non-decreasing one, meaning at every consecutive pair of positions after the operations. Negative and equal values may occur.
Find the minimum number of operations needed. Output only this number, not the sorted sequence.
Input
The first line contains , the number of elements.
The second line contains exactly integers in their initial order.
Output
Print one integer: the minimum number of adjacent swaps.
Subtasks
-
Subtask 1 (20 points): ; for every element.
-
Subtask 2 (30 points): ; for every element.
-
Subtask 3 (50 points): ; for every element.
Examples
Input
4
2 8 0 3
Output
3
Explanation
Starting from , three swaps are sufficient:
- Swap and : .
- Swap and : .
- Swap and : .
The result is non-decreasing. Initially there are exactly three inverted value pairs: , , and . Each adjacent swap can eliminate at most one inversion, so fewer than three operations cannot suffice. Output 3.