Selection Sort Movement Distance
InterviewTime limit5sMemory limit1024 MB
Given a permutation, run selection sort and report the total distance each value moves across all swaps.
- Level
Easy3 of 10
- Topics
- Array, Simulation
- Solved
- No attempts yet
Problem
A sequence of length contains each integer from 1 to exactly once. Apply the selection sort algorithm to and find the moving distance of each number.
If you are not familiar with selection sort, read the description below.
- Selection sort sorts a sequence in ascending order by repeating the following operation times.
- In the -th operation, find the minimum among .
- Swap the positions of and . The moving distances of and each increase by .
For example, take . Initially every number has moving distance 0. The algorithm runs as follows.
- Swap with itself, giving . The moving distance of 1 increases by 0.
- Swap with , giving . The moving distances of 2 and 3 each increase by 2.
- Swap with , giving . The moving distances of 3 and 5 each increase by 1.
- Swap with , giving . The moving distances of 4 and 5 each increase by 1.
So 1 moves 0, 2 moves 2, 3 moves 3, 4 moves 1, and 5 moves 2.
Input
The first line contains the length of the sequence.
The second line contains the elements of the sequence, separated by spaces, in order.
Output
Print integers separated by spaces on the first line. The -th integer is the moving distance of .
Constraints
- Each integer from to appears exactly once in .