1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <cstdio> #include <cstring> #include <iostream> #include <algorithm>
using namespace std;
const int N = 10010;
int n, m; int q[N];
int main() { scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
while (m -- ) { int k = n - 1; while (q[k - 1] > q[k]) k -- ; k -- ; int t = k; while (t + 1 < n && q[t + 1] > q[k]) t ++ ; swap(q[t], q[k]); reverse(q + k + 1, q + n); }
for (int i = 0; i < n; i ++ ) printf("%d ", q[i]);
return 0; }
|