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

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

I had been participating in contests for a long time, but something strange kept happening to me.

In some specific questions (particularly those involving hash maps or dictionaries), I always take contests in Python.
When I submitted solutions for questions involving hash maps, some of them would get accepted, but for others, I would get a TLE (Time Limit Exceeded).

Sometimes these questions would pass the pretests but then fail during system testing or higher test cases with a TLE. This same incident happened to my friend as well. When this first happened to me, I thought maybe my logic was incorrect or that I had solved it wrong. But then, when I checked solutions on streams like those of Shyaan or Aryancc, I found that my logic was correct. So, what was the issue?

One of my friends then suggested that I try solving the problem in C++.** So, I converted the same code to C++** and submitted it, and it worked for the exact same question that had given a TLE in Python.

In my last contest([contest:Hello 2025]), while solving Question B(2057B - Gorilla and the Exam) (another question involving hash maps), I submitted it in Python(299618966), and it passed the pretests. Then, suddenly, I remembered my previous experience with this issue. To be safe, I converted the same code into C++ and submitted it(299648513).

The contest results showed that my C++ submission for Question B was accepted. Out of curiosity, I submitted the same Python code afterward, and it gave a TLE on Test Case 17(299728582). That’s when I felt relieved, knowing I had done the right thing by submitting the C++ version during the contest.

In conclusion, I say this: Never trust Python dictionaries—they can betray you. So, can anyone help me with this? Because I really like submitting code in Python. Also, has this kind of incident ever happened to you as well?

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

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

I got the same issue with c++. unordered_map which is considered to be faster gave TLE(299715651). The same with map, which is ordered and has a O(log(n)) overall complexity passed the verdict(299715692)

»
16 месяцев назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

in CP Python is trash language

»
16 месяцев назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

https://mirror.codeforces.com/contest/2057/submission/299628938

If it's anything, I still sometimes forget about Python dicts sometimes being like this. If it's anything, C++ also can have a similar issue with unordered_map https://mirror.codeforces.com/blog/entry/62393.