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.
let 7,8,12 then to make all the numbers equal, we’ll have to keep 12 as it is and increase the others ,otherwise if we’ll increase all the numbers then, they never gonna be equal .
7,8,12 increase first and second number
8,9,12 increase first and second number
9,10,12 increase first and second number
10,11,12 increase first and second number
11,12,12 increase only first number
12,12,12 all are equal.
so keep the max constant and increase all the other numbers or
in other words we can say decrease the max by 1 and then increase all the numbers by 1. max-1+1=max