#Lutece0544. Bubble Sort
Bubble Sort
Migrated from Lutece 544 Bubble Sort
All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.
Description
Bubble sort is one of the simplest sorting algorithms. It works by repeatedly iterating with the array to be sorted, comparing two adjacent numbers in the array, and swapping them if they are in the wrong order. The iterations with the list are repeated until no swap is needed, which means that the array is sorted. The code below will show you how bubble sort works:
Now, give you an permutation of , we can easily use bubble sort to sort them. But we are interested more in how many times we will swap two adjacent numbers, and what's more, we want to find a permutation of to fit in the two limitation below:
- the times that swapping when bubble sort it, is the same as the original permutation.
- among all the permutations which fit in the first limitation, we need the one which has the smallest lexicographical order.
The lexicographical order is define as follow: for two permutations and , if and (), then the lexicographical order of is smaller than . For example, 1 2 3
is smaller than 1 3 2
, and 1 3 2
is smaller than 2 1 3
.
Input
First line of the input is a single integer (), indicates there are test cases.
For each test case, the first line is an integer (), the second line contains a permutation of indicates the original permutation.
Output
For each case, you should output two lines.
First line you should output the number of swaps the original permutation needed.
Second line should contain a permutation of , indicate the permutation we want.
Samples
Note
There is no space after the last number.
Resources
elfness