题目链接:火星人
知识点
next_permutation的使用。注:C语言里面没有这个函数。
next_permutation是求一个序列的全排列中,比当前序列字典序稍大的一个序列(唯一)。
例如 1 2 3 4 5 的next_permutation是1 2 3 5 4,再来一次next_permutation是1 2 5 3 4.
假若一直进行next_permutation的话,会直到变成 5 4 3 2 1(当前序列字典序最大的情况)
本题题意就是一个序列在全排列中按照字典序的大小来排序,映射成1,2,3,…。
因此用next_permutation正好。
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m;
int a[N];
int main()
{
cin >> n >> m;
string s,t;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) next_permutation(a, a + n);
for (int i = 0; i < n; i++) cout << a[i] << ' ';
}