Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void swapSwap( int a[], int k, int n ) {
- for( int i = 0; (i < n - 1) && (k > 0) ; i++ ) {
- int currMax = i;
- for( int j = i+1; j < n; j++ ) {
- if (k < j - i)
- break;
- if (a[j] > a[currMax])
- currMax = j;
- }
- for (int j = currMax; j > i; j--)
- swap(a[j], a[j - 1]);
- k -= currMax - i;
- }
- //print solution
- for( int i = 0; i < n; i++ ) {
- cout<<a[i]<<" ";
- }
- }
- int main() {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- int n,k;
- cin>>n>>k;
- int a[n];
- for(int i = 0; i < n; i++ ) {
- cin>>a[i];
- }
- swapSwap(a, k, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement