There is nothing wrong in the code. You simply need to avoid using floating points arithmetics.
Here is a reference on how to solve the problems with integers. https://ideone.com/snAUxE > My Python3 code produces the correct result for the sample input, > but gives 'Wrong answer' for the 1st test set. Can anyone give me > a hint of how to fix it? > > As for the algorithm, I use a priority queue > to give priority to percentages whose fractional part is as > close as possible to > 0.5 and don't touch percentages whose fractional part > is already >= 0.5. > > > > > > from > heapq import heappop, heappush > > > > > > def main(): > > T = int(input()) # the number of test cases > > > > for case in range(1, T+1): > > total_people, num_languages = map(int, > input().split()) > > freq = map(int, input().split()) > > > > percent_per_person = 100/total_people > > low_scores = [] > > not_responded = total_people > > res = 0 > > > > for f in freq: > > p = f*percent_per_person > > if 0 < p-int(p) < 0.5: > > heappush(low_scores, (-(p-int(p)), p)) > > else: > > res += round(p) > > not_responded -= f > > > > while not_responded: > > try: > > diff, p = heappop(low_scores) > > except IndexError: > > p = 0 > > > > p += percent_per_person > > > > if 0 < p-int(p) < 0.5: > > heappush(low_scores, (-(p-int(p)), p)) > > else: > > res += round(p) > > not_responded -= 1 > > > > res += sum(round(x[1]) for x in low_scores) > > > > print('Case #{}: {}'.format(case, res)) > > > > main() > > > -- > Regards, > Eugene -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/google-code/eaf3bdc4-8af0-4a67-bc6b-09322a159d0b%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
