Блог пользователя drugkeeper

Автор drugkeeper, история, 19 месяцев назад, По-английски

I was doing G1 (https://mirror.codeforces.com/contest/1822/problem/G1) and came up with this solution which is O(n * sqrt(m)) as stated in the editorial. This TLEs in testcase 13 due to constant factors.

TLE Code 1

I went to further optimise my code as shown here, which TLEs in testcase 16:

TLE Code 2

Main changes:

  1. Use faster output
  2. Use Counter instead of array to count, which is faster (this helped the most, and got me past testcase 13). In another solution, I initialised the count array at the very start instead of for each testcase which helped me pass everything.
  3. Got rid of unnecessary variables, if else checks and array / counter access.
  4. Use for loop instead of while loop

However, this still TLEs due to Testcase 16, which is a hash hack for Counter. If I sort the input beforehand, my code passes!

Code that works

Note that Python is too slow to pass all testcases (it fails at testcase 13), but PyPy works.

I have a few questions for the people well versed in python:

  1. Why does sorting the input help get rid of the Counter hash hack testcase TLE?
  2. Is it possible to make Python pass all testcases? (Edit: I found a submission that works in python https://mirror.codeforces.com/contest/1822/submission/203361435)

I hope you can answer my questions, if not, I hope you have learnt something from my blog. Thank you!

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
19 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by drugkeeper (previous revision, new revision, compare).

»
19 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by drugkeeper (previous revision, new revision, compare).

»
19 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Interesting sidenote: I saw that there were no Python solutions that are accepted for G2. So I found the fastest solution in PyPy:

https://mirror.codeforces.com/contest/1822/submission/204097417

Then I submitted the code, this TLEs on testcase 2:

https://mirror.codeforces.com/contest/1822/submission/204534813

After adding main() function, it passes all testcases:

https://mirror.codeforces.com/contest/1822/submission/204534896