Not only STL
Problem Description
As you know in STL there is a function called next_permutation, when you send a array to it, it will Rearranges the elements in the array into the next lexicographically greater permutation.
But today we need a next_k_permutation, you may say we can just apply k times next_permutation function to the array. Yes, you are right, but when k is very large it is not efficient enough.
So you are expected to write a program when given an array and k to find its next_k_permutation.
Note the next permutation of the permutation with largest dictionary order is the permutation with the minimum dictionary order. For example next permutation of (3,2,1) is (1,2,3).
Input
Multi test cases(about 1000). Each case contains two lines. The first line contains two integers n which indicates the length of the permutation and k. The second line contains n integers a1,a2,a3,…,an Process to the end of file.
1≤n≤2000
1≤k≤1012
1≤ai≤2000
Output
For each case, output the answer in a single line.
Sample Input
3 1
1 2 3
3 1
1 1 2
Sample Output
1 3 2
1 2 1
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答
2条回答
为你推荐
- 使用unmarshal从响应中获取特定的JSON字段
- json
- 2个回答
- Not only STL
- each
- stl
- 2个回答
- 为什么QT需要封装QString,而不需要封装 int double ???? 真心求解
- qt qstring
- 1个回答