PKU 1833 (Arrangement) STL Application

Source: Internet
Author: User

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 ();

Contact Us

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.