This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int q[N];
int n, m;
int main() {
cin >> n >> m;
for (int i = 0; i <= n; i++) cin >> q[i];
while (m--) { // 执行 m次
int k = n - 1; // 从后向前来
/*
Q:123654321 问下一个是啥?
算法步骤:
1、从向向前找第一个下降点,本例就是6
*/
while (q[k - 1] > q[k]) k--;
// 2、 k--:找到3
int t = k--;
// 3、从3开始,向后找第一个大于3的数字,就是4
while (t + 1 < n && q[t + 1] > q[k]) t++;
// 4、交换3和4
swap(q[t], q[k]);
// 5、对于折点的后半部进行翻转,即 124 653321 =>翻转=> 124 123356
reverse(q + k + 1, q + n);
}
for (int i = 0; i < n; i++) printf("%d ", q[i]);
return 0;