Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int cc(vector<int> &arr, int L, int R){
- int co = 0, co2 = 0;
- int length = R-L+1;
- vector<int> temp(length);
- for(int i = 0 ; i < length ; i++){
- int h = L+i;
- temp[i] = arr[h];
- }
- auto m = temp.begin() + temp.size()/2;
- nth_element(temp.begin(), m, temp.end());
- int mid = temp[(temp.size()-1)/2];
- for(int i = 0; i < (int)temp.size() ; i++){
- if(temp[i] <= mid)
- co += mid-temp[i];
- else
- co += temp[i]-mid;
- }
- mid = temp[((temp.size()-1)/2)+1];
- for(int i = 0; i < (int)temp.size() ; i++){
- if(temp[i] <= mid)
- co2 += mid-temp[i];
- else
- co2 += temp[i]-mid;
- }
- return min(co, co2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement