POJ 1833 - 排列 (STL)

版权声明:欢迎转载 https://blog.csdn.net/l18339702017/article/details/82469738

排列

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 21834   Accepted: 8171

Description

题目描述: 
大家知道,给出正整数n,则1到n这n个数可以构成n!种排列,把这些排列按照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1六个排列。 

任务描述: 
给出某个排列,求出这个排列的下k个排列,如果遇到最后一个排列,则下1排列为第1个排列,即排列1 2 3…n。 
比如:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。 

Input

第一行是一个正整数m,表示测试数据的个数,下面是m组测试数据,每组测试数据第一行是2个正整数n( 1 <= n < 1024 )和k(1<=k<=64),第二行有n个正整数,是1,2 … n的一个排列。

Output

对于每组输入数据,输出一行,n个数,中间用空格隔开,表示输入排列的下k个排列。

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

Source

需要加上输出挂

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define rep(i,x,y) for(int i = x; i < y; i++)
#define Rep(i,x,y) for(int i = x; i <= y; i++)
#define per(i,x,y) for(int i = x; i >= y; i--)
#define line cout << "------------" << endl

typedef long long ll;
const int maxn = 1e5 + 10;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1050;



int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        int a[N];
        int n, k;
        scanf("%d%d", &n, &k);
        for(int i=0; i<n; i++) scanf("%d", &a[i]);
        while(k){
            do{
                k--;
            }while(next_permutation(a, a+n) && k);
            if(k) sort(a, a+n);
        }
        copy(a, a+n-1, ostream_iterator<int>(cout," "));
        cout << a[n-1] <<endl;

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/82469738