Arranged
http://poj.org/problem?id=1833
Time limit:1000ms
Memory limit:30000k
Description
Topic Description:
As you know, given a positive integer n, the number of n 1 to n can constitute n! , listing these permutations in a small to large order (dictionary order), such as n=3, listing 1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 16 permutations.
Task Description:
Give an arrangement, find the arrangement of the following K, and if the last arrangement is encountered, the next 1 rows are ranked 1th, that is, 1 2 3...n.
For example: n = 3,k=2 gives an arrangement of 2 3 1, its bottom 1 arranges 3 1 2, and the next 2 is ranked 3 2 1, so the answer is 3 2 1.
Input
The first line is a positive integer m, represents the number of test data, the following is the M group of test data, the first row of each set of test data is 2 positive integers n (1 <= N < 1024) and K (1<=k<=64), the second row has n positive integers, is 1,2 ... an arrangement of N.
Output
For each group of input data, output a row, n number, separated by a space in the middle, representing the input arrangement of the next K arrangement.
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
Complete code:
/*469ms,168kb*/
#include <cstdio>
#include <algorithm>
using namespace std;
int num[1024];
int main (void)
{
int m, n, K;
scanf ("%d", &m);
while (m--)
{
scanf ("%d%d", &n, &k);
for (int i = 0; i < n; ++i)
scanf ("%d", &num[i]);
while (k--)
next_permutation (num, num + N);
printf ("%d", num[0]);
for (int i = 1; i < n; ++i)
printf ("%d", Num[i]);
Putchar (' \ n ');
}
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/