z-sorting
Mister Little Z. wrote some nice and fast method for sorting competition results. His method takes all the scores and sorts them in decreasing order.
However, some hackers wanted to mess up with mister Little Z's website, so they hacked his code so that after the sorting is done, some exchanges are made. For example if the sorted array is "4 3 2 1", Hacker's code will transform this list to "4 1 3 2". in this case the following exchanges are made: from position 2 to position 4, then from position 4 to position 3. Note that multiple exchange operations could lead to the same result.
You are given the results of one competition, where all the scores are different. The results are sorted by Little's Z sorting algorithm, and then, hacker's code has exchanged some results. You are also given the scores from the next competition (same number of scores but not sorted at all), your job is to simulate Z's sorting algorithm, and then simulate hacker's algorithm, and predict what will be shown on the website.
Input:
4
4 1 3 2
1 8 4 2Output:
8 1 4 2Explanation, after Z's sorting the scores will be: 8 4 2 1, and then we apply hackers exchanges, and obtain 8 1 4 2
Input:
3
10 20 30
8 4 7Output:
4 7 8Explanation, after Z's sorting the scores will be: 8 7 4. We see that the hackers apply such a set of exchanges that the array ends up being sorted in an increasing order.
Submit solution
Coming laterThe grading service will be connected in a later migration step. You can inspect the task and your previous results now.