# K sorting after POJ1833 Problem Solving report

Source: Internet
Author: User
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.

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;}`
Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.