Previous Permutation

Problem Description

Given a permutation A of 1...N(N<=20), now you need to find the K-previous permutation of A. We said that a
permutation B is the 1-pervious permutation of A if and only if the next permutation of B is A in numerical order. 
In this way we can define 2-pervious permutation, 3-pervious permutation, etc. Please keep in mind that the 
permutation (1,2,...,N) does NOT have any pervious ermutations.

Input

Input contains multiple test cases. For each test cases, the first line of input contains two integers: N and 
K(0<K<=10^9), then the second line contains a 
permutation of 1...N. Input is terminated by a single line contains two zeros.

Output

For each test case output the needed permutation in a single line. In case of none existence, just output -1 
in a single line.

Sample Input

5 4
4 1 3 2 5
5 1
1 2 3 4 5
5 119
5 4 3 2 1
0 0

Sample Output

3 5 4 1 2
-1
1 2 3 4 5
/*   
       题解: 由{1,2,3,4,5} - > {4,1,3,2,5};
              首先:{1,2,3,4,5};
              第一位:1->4需要走3*(5-1)!;{1,2,3,5};
              第二位:2->1需要走0*(5-2)!;{2,3,5};
              第三位:3->3需要走1*(5-3)!;{2,5};
              第四位:2->4需要走0*(5-4)!;{2};
              第五位:5->5需要走0*(5-5)!;{};
              一共需要:72+0+2+0+0 = 74步。
              而我们所求的就是从{1,2,3,4,5}走(74-k)步后的排列。
              首先:{1,2,3,4,5};
              temp = 74-k = 70;
              第一位:temp/((5-1)!)=2, temp = temp-2*(5-1)!=22;
                      {3},{1,2,4,5};
              第二位:temp/((5-2)!)=3, temp = temp-3*(5-2)!=4;
                      {3,5},{1,2,4};
              第三位:temp/((5-3)!)=2, temp = temp-2*(5-3)!=0;
                      {3,5,4},{1,2};
              第四位:temp/((5-2)!)=0, temp = temp-0*(5-4)!=0;
                      {3,5,4,1},{2};
              第五位:temp/((5-2)!)=3, temp = temp-0*(5-5)!=0;
                      {3,5,4,1,2},{};
*/
//标程:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int b[22], vis[22];
int main()
{
//  freopen("a.txt","r",stdin);
    int i, n, j, cnt;
    __int64 a[22];
    a[0] = 1;
    for(i = 1; i <= 20; i ++)
         a[i] = a[i-1]*i;
    __int64 k;
    while(scanf("%d%I64d",&n,&k),n+k)
    {
        for(i = 1; i <= n; i ++)
            cin >> b[i];
        memset(vis,0,sizeof(vis));
        __int64 temp = 0;
        for(i = 1; i <= n; i ++)
        {
            cnt = 0;
            for(j = 1; j <= n; j ++)
            {
                if(b[i] == j)
                {
                     temp += cnt*a[n-i];
                     vis[j] = 1;
                }
                if(vis[j]==0) cnt ++;
            }
        }
        temp -= k;
        if(temp < 0)  
		{
		    printf("-1\n"); 
			continue;
		}
        int c[22];
        memset(c,0,sizeof(c));
        for(i = n-1; i >= 0; i --)
        {
            for(j = 1; j <= n; j ++)
                if(j*a[i] > temp) break;
            temp = temp - (j-1)*a[i];
            for(int x = 1; x <= n; x ++)
            {
                if(vis[x] == 1)  j --;
                if(j == 0) { vis[x] = 0; c[n-i] = x; break; }
            }
        }
        for(i = 1; i < n; i ++)
            printf("%d ",c[i]);
        printf("%d\n",c[n]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值