A few hours ago, the hacking phase for the round (Div. 3) ended and the system testing began. I wanted to check how many solutions failed the system tests, and I saw a huge number of TLEs on problem C. After looking at several solutions, I realized that they all had one thing in common — unordered_map.
I think many people know that unordered_map (hereafter UM) can work in both $$$O(1)$$$ and $$$O(n)$$$ time. Because of this, some people found a test case where UM runs in $$$O(n)$$$, which makes the overall complexity $$$O(n^2)$$$.
It got me wondering why people massively use UM when a regular map passes within the time limits. I asked ChatGPT to solve this problem, and it gave me a solution using UM.
On the one hand, it’s kind of funny that people who copied from ChatGPT (or another LLM) got their solutions rejected.
On the other hand, there are people who wrote the code themselves but decided to use UM instead of map (although I think that’s their own fault because they could have just used a regular map).








This is hilarious and a good entertainment too. That is, to go to the status page, pick problem C and check the TLE submissions only to see, that contestants with such submissions follow... a certain pattern, you know.
GPT-5 is doing wonders since its launch
Honestly I was scared with a normal map, looking at the time it took during contest and seriously considered resubmitting, thank god i didn't
I think you need to learn how to calculate complexity correctly. Because O(nlogn) is not scary when n<=2e5
I like to use map :)
It's short and rarely gets hacked.
true that, i have never used unordered map
So do I xD
I believe this will pass if you sort the array before hand
I am a beginner so I don't if I have a place to speak here lulw but I use unordered_map like all the time whenever I have to use something sort of a hashmap. why is it preferred to use a normal map over unordered_map? does the sorting provide some sort of a powerup?
The reason
mapis preferred overunordered_mapis because, when you useumap[x], it uses the hash function (default:std::hash) and some other stuff to check ifumap[x]exists and returns the value if so. But if there's a hash collision (two keys inumap, sayxandy) it takes up to $$$O(n)$$$ to resovle the collision and get the corresponding value. Carefully selected keys can result in a time complexity as high as $$$O(n^{2})$$$.Ah ok, so ordered_map doesn't have this problem then? if that's so, shouldn't the normal ordered_map be used in every situation?
std::maptakes $$$O(\log n)$$$ operations where $$$n$$$ is the number of elements in the map.The reason people (and ChatGPT) use
std::unordered_mapdoes $$$O(1)$$$ operations, and so is generally faster thanstd::map, except when hash collisions happen. Some people also don't know about the hash collisions part and don't use a custom, stronger hash which lead to hash collisions with GCC's defaultstd::hashandstd::unordered_map.Read this blog on
std::unordered_mapfor preventing most hash collisions. Basically, if you useand
unordered_map<int, int, custom_hash>, hash collisions will become much less likely.No human would ever use
unordered_map.reservein a contestWhy not? If I'm using unordered_map, it means I'm already concerned about performance, and reserve might speed up the code significantly and has no downsides, so practically no reason (at least I'm not aware of any) not to use it
Why not? It prevents rebuilding hash map as it grows. Thus, some contestants may think that executing the reserve method may improve the performance
Why do you think so? That method doesn't seem to be something unusual. With approximate understanding on how UM works and what does
unordered_map.reservedo the UM can become a powerful enough tool.