I ask only the algorithm to use

enter image description here

Closed due to the fact that the issue is too general for participants jfs , Abyx , Lex Hobbit , default locale , Komdosh Oct 31 '17 at 6:43 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    You copy the task with the text and paste it here, not with a screenshot. - Alexey Shimansky
  • I have now only a screenshot - Alone_Fox
  • First we find the sum of all the elements. Divide by quantity. Thus we obtain the average value by which it is necessary to replace all the elements. If the sum of the elements is not divided by the quantity, immediately output -1. We go through the array. If the element is greater than or equal to the average, then skip it. If it is less than the average, then we need to supplement it to the average. And for this you need to somehow find the nearest element on the left which is more than the average and on the right. And change the elements. But I don’t know how to do it quickly = (Perhaps it’s necessary to use data structures of the segment tree type. - moskalenco_a
  • By the way, it is interesting that theoretically the condition does not prohibit the deletion of extreme columns at all, no? - Harry
  • @Harry the number of columns cannot be changed - Alone_Fox

1 answer 1

As an option:

lt find_way(lt n,vector<lt>& pt){ if(n == 1) return 0; lt b = 0; lt i = 0; lt j = 0; lt k = 0; lt left = 0; lt right = 0; while(b != n){ // print(pt); i = find_min(pt); //index j = find_max(pt); if(i != j){ pt[j]--; if(i < j){ j--; } else{ j++; } pt[j]++; swap(left,right); if(left == pt[i] && right == pt[j]){ return -1; } } else{ return k; } left = pt[i]; right = pt[j]; k++; b = proverka(pt); //counting p[0] == p[i] } return k; } 
  • It should not be tested, so there may be errors ... - vados inferno
  • What libraries other than vector did you use? If I correctly understand this function? What does it mean? - Alone_Fox
  • using lt = long int; In addition to the vector, I did not use anything else. Yes, I gave you a function that includes other functions (signed what they do). - vados inferno