#K70597. Permutation Extremes
Permutation Extremes
Permutation Extremes
Given a positive integer \( n \), generate two permutations of the numbers from 1 to \( n \): one that minimizes the sum of absolute differences between consecutive elements, and another that maximizes it.
The minimal permutation is simply the sorted sequence: \( [1, 2, \dots, n] \), because the absolute difference between each consecutive pair is minimized (each difference is 1, summing to \( n-1 \)).
The maximal permutation can be formed by alternately choosing the smallest and largest remaining numbers. Formally, if the permutation is \( P = (p_1, p_2, \dots, p_n) \), then the sum to maximize is \( S = \sum_{i=1}^{n-1} |p_{i+1} - p_i| \). The strategy is to arrange the numbers to ensure the differences are as high as possible.
inputFormat
The first line contains a single integer \( T \) (the number of test cases). Each of the following \( T \) lines contains a single integer \( n \) \( (1 \le n \le 10^5) \) representing a test case.
outputFormat
For each test case, print two lines:
- The first line should contain the minimal permutation of \( n \) as \( n \) space-separated integers.
- The second line should contain the maximal permutation of \( n \) as \( n \) space-separated integers.
1
5
1 2 3 4 5
1 5 2 4 3
</p>