#C3086. Lexicographically Smallest Array with Limited Reversals

    ID: 46474 Type: Default 1000ms 256MiB

Lexicographically Smallest Array with Limited Reversals

Lexicographically Smallest Array with Limited Reversals

You are given T test cases. In each test case, you are provided with an array of N integers and an integer K representing the number of allowed reversal operations.

If K > 1, you are allowed to perform more than one reversal operation, and hence you can always achieve the lexicographically smallest array by sorting the array in non-decreasing order.

If K = 1, you are allowed to perform exactly one reversal operation. That is, you can choose a contiguous subarray and reverse it once. Your goal is to determine the lexicographically smallest array that can be obtained by applying at most that one reversal operation.

The lexicographic order between two arrays is determined by comparing their elements from left to right. Formally, an array A is lexicographically smaller than an array B if there exists an index i such that A[i] < B[i] and for all j where 0 ≤ j < i, A[j] = B[j].

Solve the problem for all test cases.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer T, the number of test cases.
  • Each test case consists of two lines:
    • The first line contains two space-separated integers N and K, where N is the size of the array and K is the number of allowed reversal operations.
    • The second line contains N space-separated integers representing the elements of the array.

Note: If K > 1, you can obtain the lexicographically smallest array by simply sorting the array.

outputFormat

For each test case, print a single line that contains the lexicographically smallest array achievable. The elements must be space-separated.

## sample
2
5 1
3 1 4 1 5
4 2
4 3 2 1
1 3 4 1 5

1 2 3 4

</p>