Advertisement
Guest User

Untitled

a guest
May 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void swapSwap( int a[], int k, int n ) {
  5. for( int i = 0; (i < n - 1) && (k > 0) ; i++ ) {
  6. int currMax = i;
  7.  
  8. for( int j = i+1; j < n; j++ ) {
  9. if (k < j - i)
  10. break;
  11. if (a[j] > a[currMax])
  12. currMax = j;
  13. }
  14.  
  15. for (int j = currMax; j > i; j--)
  16. swap(a[j], a[j - 1]);
  17.  
  18. k -= currMax - i;
  19. }
  20.  
  21. //print solution
  22. for( int i = 0; i < n; i++ ) {
  23. cout<<a[i]<<" ";
  24. }
  25. }
  26. int main() {
  27.  
  28. #ifndef ONLINE_JUDGE
  29. freopen("input.txt", "r", stdin);
  30. freopen("output.txt", "w", stdout);
  31. #endif
  32. int n,k;
  33. cin>>n>>k;
  34. int a[n];
  35.  
  36. for(int i = 0; i < n; i++ ) {
  37. cin>>a[i];
  38. }
  39.  
  40. swapSwap(a, k, n);
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement