You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
890 B

2 years ago
#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
16
*/
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;
}