• Source
    1. #include <iostream>
    2. #include <vector>
    3. using namespace std;
    4.  
    5. int main()
    6. {
    7. int n,k;
    8. cin>>n>>k;
    9. vector<int> a(n), pos(n+1);
    10. for(int i = 0; i < n; ++i){
    11. cin>>a[i];
    12. pos[ a[i] ] = i;
    13. }
    14. for(int i = 0; i < n && k > 0; ++i){
    15. int maxInd = i, maxElem = a[i];
    16. for(int j = i+1; j < n; ++j){
    17. if( a[j] > maxElem){
    18. maxElem = a[j];
    19. maxInd = j;
    20. }
    21. }
    22. if(i != maxInd){
    23. --k;
    24. swap( a[i], a[maxInd]);
    25. }
    26. }
    27. for(int i = 0; i < n; ++i){
    28. cout<<a[i]<<" ";
    29. }
    30. // your code goes here
    31. return 0;
    32. }