Created
May 11, 2019 15:56
-
-
Save dsc712/5038a7222fda2ae257b2141e3b7436d9 to your computer and use it in GitHub Desktop.
swap swap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment