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.
I went to further optimise my code as shown here, which TLEs in testcase 16:
Main changes:
- Use faster output
- 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.
- Got rid of unnecessary variables, if else checks and array / counter access.
- 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!
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:
- Why does sorting the input help get rid of the Counter hash hack testcase TLE?
- 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!
Auto comment: topic has been updated by drugkeeper (previous revision, new revision, compare).
Auto comment: topic has been updated by drugkeeper (previous revision, new revision, compare).
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