Description
Description:
As you know, given a positive integer N, the N numbers from 1 to n can constitute n! Sort, which are listed in alphabetical order, for example, when n = 3, lists the six arrays of 1 2, 1 3, 2, 2, 3, 1, 3, 2, and 1.
Task Description:
Give an arrangement and find the next K arrangement of the arrangement. If the last arrangement is encountered, the next 1 is arranged as 1st, that is, the Arrangement 1 2 3... N.
For example, if n = 3, K = 2 gives an arrangement of 2 3 1, the next one is 3 1 2, and the next two are 3 2 1, therefore, the answer is 3 2 1.
Input
The first row is a positive integer m, which indicates the number of test data. below is the test data of group M, the first row of each group of test data is two positive integers, n (1 <= n <1024) and K (1 <= k <= 64). The second row has n positive integers, is 1, 2... N.
Output
For each group of input data, output a row with N numbers separated by spaces, indicating the next k orders in the input arrangement.
Sample Input
33 12 3 13 13 2 110 21 2 3 4 5 6 7 8 9 10
Sample output
3 1 21 2 31 2 3 4 5 6 7 9 8 10# Include <iostream> # include <algorithm> using namespace STD; int main () {int ncase, N, K, I; int s [1024]; scanf ("% d", & ncase); While (ncase --) {scanf ("% d", & N, & K); for (I = 0; I <n; I ++) {scanf ("% d", & S [I]) ;}for (I = 0; I <K; I ++) {next_permutation (S, S + n) ;}for (I = 0; I <n-1; I ++) {printf ("% d", s [I]);} printf ("% d/N", s [I]);} return 0 ;}
Today I met another STL function next_permutation ();