#FT0000196. Bốn phương pháp sắp xếp (Sorting Methods)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bốn phương pháp sắp xếp (Sorting Methods)
Sorting Methods
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
Given a permutation of the integers from to , transform it into .
Consider four independent methods, each permitting repeated operations:
- Swap two adjacent elements.
- Swap any two elements.
- Remove one element and insert it at any position, preserving the relative order of all remaining elements.
- Remove one element and insert it at the front, preserving the relative order of all remaining elements.
For each method, determine its minimum number of operations. Every method starts with the original permutation, independently of all others.
Input
- The first line contains an integer , the permutation length.
- The second line contains integers , each integer from to appearing exactly once.
Output
Print one line containing four integers, in the order of methods , , , . Each is the minimum number of operations for that method.
Subtasks
-
Subtask 1 (20 points): ; is a permutation of .
-
Subtask 2 (30 points): ; is a permutation of .
-
Subtask 3 (50 points): ; is a permutation of .
Examples
Input
8
7 8 2 6 5 1 3 4
Output
20 6 5 6
Explanation
Initially . There are inversions, giving adjacent swaps. The cycle decomposition has one cycle of length and the fixed element , so arbitrary swaps require operations. An increasing subsequence of length , such as , can be left in place relative to the other retained elements; therefore moves are required. Finally, the target suffix already appears in order, so moving successively to the front sorts the permutation in moves. A longer retained target suffix is impossible.