Accept:304 | Submit:994 |
Time Limit:1000MS | Memory Limit:65536KB |
Description
We all know that , when there are n positive integers (namely 1…n) , we can use these n integers to construct n! kinds of permutations . Sort these permutations in lexicographic order . For example , when n = 3 , we can permute 1 2 3,1 3 2,2 1 3, 2 3 1, 3 1 2 , 3 2 1 these 6 kinds of permutations.
Now , we want to know , with the given permutation , calculate the next k permutation . If the permutation is the last permutation in order , its next permutation is the first permutation , namely 1 2 3 … n.
e.g. when n = 3 , k = 2 , giving the permutation 2 3 1 , thus its next 1 permutation is 3 1 2 , its next 2 permutation is 3 2 1 , so the answer is 3 2 1 .
Input
The first line is a positive integer m , indicating the number of test cases . Then m test cases . In every test case , the first line is 2 positive integers n (1 <= n < 1024) and k (1 <= k <= 64) ; the second line are n positive integers , which is one of the “ 1 2 3 … n” s’ permutation.
Output
For each test case , print one line , n numbers , with spaces separating every number , indicating the given permutation ‘s next k permutation.
Sample Input
3
3 1
2 3 1
3 1
3 2 1
10 2
1 2 3 4 5 6 7 8 9 10
Sample Output
3 1 2
1 2 3
1 2 3 4 5 6 7 9 8 10
/*BOJ No.5-Permutation生成下k位的字典序,强大的库函数*/
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,i,k,t;
int myints[1050];
cin>>t;
while(t--)
{
cin>>n>>k;
for(i=0; i<n; i++)
cin>>myints[i];
while(k--)
next_permutation (myints,myints+n);
for(i=0; i<n-1; i++)
cout<<myints[i]<<"";
cout<<myints[n-1]<<endl;
}
return 0;
}