#B4171. Selection Sort Rounds
Selection Sort Rounds
Selection Sort Rounds
In this problem, you are to simulate the selection sort algorithm. Selection sort works as follows: for each round \( i \) (where \(1 \le i \le n-1\)), the algorithm finds the smallest element in the subarray \(A[i \sim n]\) and swaps it with \(A[i]\). After \(n-1\) rounds, the array is sorted in ascending order.
For example, given \(A = [3, 4, 1, 5, 2]\):
- Round 1: Swap \(A[1]\) and \(A[3]\), the array becomes \([1, 4, 3, 5, 2]\).
- Round 2: Swap \(A[2]\) and \(A[5]\), the array becomes \([1, 2, 3, 5, 4]\).
- Round 3: Swap \(A[3]\) with itself, the array remains \([1, 2, 3, 5, 4]\).
- Round 4: Swap \(A[4]\) and \(A[5]\), the array becomes \([1, 2, 3, 4, 5]\).
You are given an initial permutation \(A[1 \sim n]\) (i.e. each of the numbers from \(1\) to \(n\) appears exactly once) and \(m\) queries \(q[1], q[2], \ldots, q[m]\) with \(q[i] < q[i+1]\). For each query, output the state of the array after the specified \(q[i]\)-th round of selection sort.
inputFormat
The first line contains two integers \(n\) and \(m\) (the length of the array and the number of queries).
The second line contains \(n\) space-separated integers representing a permutation of \(1\) to \(n\).
The following \(m\) lines each contain one integer \(q[i]\), representing a query for the state of the array after the \(q[i]\)-th round of selection sort.
outputFormat
For each query, output one line with \(n\) integers representing the state of the array after the specified round of selection sort. The integers in each line should be separated by a space.
sample
5 2
3 4 1 5 2
1
3
1 4 3 5 2
1 2 3 5 4
</p>