Advertisement
Josif_tepe

Untitled

Dec 9th, 2022
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4.  
  5. using namespace std;
  6. int main()
  7. {
  8.     int n, k;
  9.     cin >> n >> k;
  10.    
  11.     int niza[n];
  12.     pair<int, int> p[n];
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> niza[i];
  15.         p[i] = make_pair(niza[i], i);
  16.     }
  17.     sort(p, p + n);
  18.     int l = 0, r = n - 1;
  19.     for(int i = 0; i < k; i++) {
  20.         swap(niza[p[l].second], niza[p[r].second]);
  21.         l++;
  22.         r--;
  23.     }
  24.     for(int i = 0; i < n; i++) {
  25.         cout << niza[i] << " ";
  26.     }
  27.     return 0;
  28. }
  29. /*
  30.  (50, 0) (100, 1) (45, 2) (2000, 3) (1300, 4)
  31. 50 100 45 2000 1300
  32.  
  33.  к = 1
  34.  (45, 2) (50, 0)  (100, 1) (1300, 4) (2000, 3)
  35.  
  36.  
  37.  **/
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement