老BOJ 05 Permutation

11 篇文章 0 订阅
10 篇文章 0 订阅
Permutation
Accept:304    Submit:994
Time Limit:1000MS    Memory Limit:65536KB

Description

 

We all know that , when there are n positive integers (namely 1…n) , we can use these n integers to construct n! kinds of permutations . Sort these permutations in lexicographic order . For example , when n = 3 , we can permute 1 2 3,1 3 2,2 1 3, 2 3 1, 3 1 2 , 3 2 1 these 6 kinds of permutations.

Now , we want to know , with the given permutation , calculate the next k permutation . If the permutation is the last permutation in order , its next permutation is the first permutation , namely 1 2 3 … n.

e.g. when n = 3 , k = 2 , giving the permutation 2 3 1 , thus its next 1 permutation is 3 1 2 , its next 2 permutation is 3 2 1 , so the answer is 3 2 1 .

Input

 

The first line is a positive integer m , indicating the number of test cases . Then m test cases . In every test case , the first line is 2 positive integers n (1 <= n < 1024) and k (1 <= k <= 64) ; the second line are n positive integers , which is one of the  “ 1 2 3 … n”  s’ permutation.

 

Output

 

For each test case , print one line , n numbers , with spaces separating every number , indicating the given permutation ‘s next k permutation.

 

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

/*BOJ No.5-Permutation生成下k位的字典序,强大的库函数*/
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,i,k,t;
    int myints[1050];
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        for(i=0; i<n; i++)
            cin>>myints[i];
        while(k--)
            next_permutation (myints,myints+n);
        for(i=0; i<n-1; i++)
            cout<<myints[i]<<"";
        cout<<myints[n-1]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值