Arrange
Time Limit:1000 MS |
|
Memory Limit:30000 K |
Total Submissions:9564 |
|
Accepted:4223 |
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
Or arrange the question, recursion 500 + ms ac... (Some people use STL 400 MS +, which is relatively cumbersome)
#include<iostream>using namespace std;int n,k,step[1100];char lock[1100];void f(int d){if(d>n){k--;return;}for(;step[d]<=n;step[d]++)if(!lock[step[d]]){lock[step[d]]=1;f(d+1);lock[step[d]]=0;if(!k)return;}step[d]=1;}int main(){int i,t;cin>>t;while(t--){cin>>n>>k;memset(lock,0,sizeof(lock));for(i=1;i<=n;i++)scanf("%d",&step[i]);k++;while(k)f(1);for(i=1;i<=n;i++)printf("%d ",step[i]);cout<<endl;}return 0;}