思路 next_permutation的实现
先找到峰顶下标k;
寻找与a[k-1]交换的值(下标为idx),进行交换;
反转a[k] ~ a[n-1];
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10010;
int a[N];
int main(){
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> a[i];
while(m--) {
int k = n - 1;//记录峰顶
while(a[k] < a[k - 1]) k--;
int idx = n - 1;//记录替换位置
while(a[idx] < a[k - 1]) idx--;
swap(a[k - 1], a[idx]);
reverse(a + k, a + n);
}
for(int i = 0; i < n; i++) {
if(i != 0) cout << ' ';
cout << a[i];
}
cout <<endl;
return 0;
}