I think you have misunderstood the analysis. To keep a reasonable complexity, 
you need to make sure that your set cannot contain several times the same 
element.
See the comment saying "This is a set, not a multiset!". Therefore, heapq will 
not be the right datastructure for this, as it does not allow to check if an 
element is in the heap easily.

So basically, you need anything resembling a set, along with a way to count the 
number of occurrences of a given element.

On Thursday, August 23, 2018 at 1:37:05 AM UTC+2, Eugene Yarmash wrote:
> Hello. I've tried to solve the problem using Python's heapq module, but 
> that gave me 'Time limit exceeded' for the 2nd test case. I've checked 
> the analysis and it claims that this approach should work for the 2nd 
> test case. Am I missing something here? My code:
> 
> from heapq import heappop, heappush
> 
> 
> def main():
>      T = int(input())  # the number of test cases
> 
>      for case in range(1, T+1):
>          N, K = map(int, input().split())
> 
>          h = [-N]
> 
>          for _ in range(K):
>              chunk = -heappop(h)
>              if chunk & 1:
>                  if chunk == 1:
>                      min_s = max_s = 0
>                  else:
>                      min_s = max_s = chunk >> 1
>                      heappush(h, -min_s)
>                      heappush(h, -max_s)
>              else:
>                  max_s = chunk >> 1
>                  min_s = max_s - 1
>                  heappush(h, -min_s)
>                  heappush(h, -max_s)
> 
>          print('Case #{}: {} {}'.format(case, max_s, min_s))
> 
> 
> 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/f5f98492-0002-432d-bd93-f9dd068d9fc8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to