@herman look this thing in this way… increasing salary of (n-1) workers is like all the (n-1) workers get 1 point each and the 1 worker left gets a 0 …but we are concerned with the number of moves we need to perform to make all salaries equal which will be equal to the scenario where we just decrease the salary of a worker by 1… i.e now instead of giving 1 point we give 0 point each to (n-1) workers and -1 point to the unlucky worker and this unlucky one is the one having salary higher than the minimum salary…
I determine the MAX of the array and the MIN. The difference of MAX and MIN gives rise to DELTA.
Then, I consider the first element with value MAX and increase the salaries of all the other elements by delta.
The above can give rise to a new set of MAX and MIN and thus DELTA. I re-compute DELTA and iterate the above operation of selecting the element with a value of MAX and increase salaries of other elements by DELTA.
This operation is repeated until DELTA becomes equal to 0. Yet I am getting a wrong answer.
Can you please see point out my mistake. This problem is very simple yet
I am using Merge Sort AFAIK its the best sorting algorithm for time complexity but still Code Chef compiler says time limit exceeded. Kindly help me out in optimising it !
@Vartult: You can’t stop 2 numbers at a time according to the question,“choose some worker and increase by 1 salary of each worker, except the salary of the chosen worker”.Here it is given as worker and not as workers. So 2 3 3 >>> 3 4 3 >>> 4 4 4
def main():
#codechef question SALARY
t = input()
t = int(t)
while t > 0:
n = input()
n = int(n)
val = list(map(int, input().split(" ")))
initial_sum = sum(val)
min_value = min(val)
left = 0
right = 10000000000
while left <= right:
mid = (left + right) // 2
may_be = initial_sum + (mid * (n-1))
mean = may_be / n
diff = mean - min_value
if diff == mid:
break
elif diff < mid:
right = mid - 1
else:
left = mid + 1
print(mid)
t -= 1
Actually you get TLE and it is expected for your solution.
The test case where your program works too slow is described in the editorial. But I repeat here:
T = 100
N = 100 for each test
salaries are {0, 10000, 10000, …, 10000} for each test
I have added DRGNBOOL and LECANDY. Yours both seems too straightforward like just do what is written in the problem statement. But thanks for your effort.
Actually, the only your mistake is that you did not print the newline after the answer for each test. Fixing this give AC: http://www.codechef.com/viewsolution/1724864
I was noticed this during the contest but, of course, I could not pointed out this to you.
Oh Dear Lord!!! Thanks anton_lunyov for answering. I was baffled whether there is something wrong with my approach because this is such an easy problem. Thanks again.