Did you know that Chwee kueh, a cuisine of Singapore, means water rice cake ? Its a…
- User
Did you know that Chwee kueh, a cuisine of Singapore, means water rice cake ? Its a variety of the most popular South Indian savory cake, only that we call it here idli :). The tastiest idlis are made in Chennai, by none other than our famous chef, Dexter Murugan. Being very popular, he is flown from Marina to Miami, to serve idlis in the opening ceremony of icpc world finals ( which is happening right now ! ). There are N students and they are initially served with some idlis. Some of them are angry because they got less idlis than some other. Dexter decides to redistribute the idlis so they all get equal number of idlis finally. He recollects his father's code, "Son, if you ever want to redistribute idlis, follow this method. While there are two persons with unequal number of idlis, repeat the following step. Select two persons A and B, A having the maximum and B having the minimum number of idlis, currently. If there are multiple ways to select A (similarly B), select any one randomly. Let A and B have P and Q number of idlis respectively and R = ceil( ( P - Q ) / 2 ), Transfer R idlis from A to B." Given the initial number of idlis served to each student, find the number of times Dexter has to repeat the above step. If he can not distribute idlis equally by following the above method, print -1.
Notes ceil(x) is the smallest integer that is not less than x. Input First line contains an integer T ( number of test cases, around 20 ). T cases follows. Each case starts with an integer N ( 1 <= N <= 3000 ). Next line contains an array A of N integers separated by spaces, the initial number of idlis served ( 0 <= A[i] <= N )
Output For each case, output the number of times Dexter has to repeat the given step to distribute idlis equally or -1 if its not possible.
Example
Input: 3 4 1 2 2 3 2 1 2 7 1 2 3 4 5 6 7 Output: 1 -1 3
Explanation: Case 1 : { 1, 2, 2, 3}. Maximum 3, Minimum 1. R = ceil((3-1)/2) = 1. Transfer 1 idli from person having 3 idlis to the person having 1 idli. Each of them has 2 idlis now, so just 1 step is enough. Case 2 : {1,2} R = ceil((2-1)/2) = 1. {1,2} -> {2,1} -> {1,2} .... they can never get equal idlis :( Case 3 : Sorted arrays, in the order encountered {1, 2, 3, 4, 5, 6, 7} -> {2, 3, 4, 4, 4, 5, 6} -> {3, 4, 4, 4, 4, 4, 5} -> {4, 4, 4, 4, 4, 4, 4}
NoteThere are multiple test sets, and the judge shows the sum of the time taken over all test sets of your submission, if Accepted. Time limit on each test set is 3 sec
Assistant
from collections import defaultdict import heapq def solve(): import sys input = sys.stdin.read().split() idx = 0 T = int(input[idx]) idx += 1 for _ in range(T): N = int(input[idx]) idx += 1 A = list(map(int, input[idx:idx+N])) idx += N total = sum(A) if total % N != 0: print(-1) continue avg = total // N freq = defaultdict(int) max_heap = [] min_heap = [] for num in A: freq[num] += 1 heapq.heappush(max_heap, -num) heapq.heappush(min_heap, num) steps = 0 while True: # Get current_max current_max = None while max_heap: candidate = -max_heap[0] if freq.get(candidate, 0) > 0: current_max = candidate break else: heapq.heappop(max_heap) # Get current_min current_min = None while min_heap: candidate = min_heap[0] if freq.get(candidate, 0) > 0: current_min = candidate break else: heapq.heappop(min_heap) if current_max == current_min: print(steps) break if current_max is None or current_min is None: print(-1) break delta = current_max - current_min R = (delta + 1) // 2 steps += 1 # Update frequencies freq[current_max] -= 1 if freq[current_max] == 0: del freq[current_max] freq[current_min] -= 1 if freq[current_min] == 0: del freq[current_min] new_max_val = current_max - R new_min_val = current_min + R freq[new_max_val] += 1 freq[new_min_val] += 1 # Push new values into heaps heapq.heappush(max_heap, -new_max_val) heapq.heappush(max_heap, -new_min_val) heapq.heappush(min_heap, new_max_val) heapq.heappush(min_heap, new_min_val) if __name__ == "__main__": solve()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.