#C7114. Minimize Absolute Differences
Minimize Absolute Differences
Minimize Absolute Differences
Given an unordered array containing n distinct integers ranging from 1 to n (i.e. a permutation), the task is to find a permutation of the array such that the sum of absolute differences between each element's position (1-indexed) and its value is minimized.
In mathematical terms, for a permutation \(p = [p_1, p_2, \dots, p_n]\), you need to minimize the sum:
\(\sum_{i=1}^{n}|p_i - i|\)
It turns out that the optimal solution is to simply sort the array in non-decreasing order; placing the smallest element at the first position, the second smallest at the second position, and so on.
You are given multiple test cases; for each test case, output a single line containing the sorted permutation.
inputFormat
The first line of input contains one integer T (\(1 \leq T\leq 10^5\)) representing the number of test cases.
For each test case:
- The first line contains an integer n (\(1 \leq n \leq 10^5\)) representing the number of elements in the permutation.
- The second line contains n space-separated integers representing a permutation of the numbers from 1 to n.
The total size of the input does not exceed reasonable limits for standard input.
outputFormat
For each test case, output a single line containing the sorted permutation of numbers from 1 to n, with each number separated by a single space.
## sample1
3
3 1 2
1 2 3
</p>