Advertisement
KuanTall

Untitled

Oct 22nd, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. int cc(vector<int> &arr, int L, int R){
  2. int co = 0, co2 = 0;
  3. int length = R-L+1;
  4. vector<int> temp(length);
  5. for(int i = 0 ; i < length ; i++){
  6. int h = L+i;
  7. temp[i] = arr[h];
  8. }
  9.  
  10. auto m = temp.begin() + temp.size()/2;
  11.  
  12. nth_element(temp.begin(), m, temp.end());
  13.  
  14. int mid = temp[(temp.size()-1)/2];
  15.  
  16. for(int i = 0; i < (int)temp.size() ; i++){
  17. if(temp[i] <= mid)
  18. co += mid-temp[i];
  19. else
  20. co += temp[i]-mid;
  21. }
  22.  
  23. mid = temp[((temp.size()-1)/2)+1];
  24.  
  25. for(int i = 0; i < (int)temp.size() ; i++){
  26. if(temp[i] <= mid)
  27. co2 += mid-temp[i];
  28. else
  29. co2 += temp[i]-mid;
  30. }
  31.  
  32.  
  33. return min(co, co2);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement