__dopamIne's blog

By __dopamIne, history, 9 months ago, In English

In competitive programming, we often use map or unordered_map. However, choosing the wrong container can sometimes cause our code to hit a Time Limit Exceeded (TLE). Today, I will share with you why the unordered_map get TLE ?

1.Hash collisions → Worst case O(n) instead of O(1). 2.Adversarial inputs → Test cases designed to break default hash. 3.String hashing cost → Large strings slow down lookups. 4.Higher constants → More memory & overhead than map.

https://mirror.codeforces.com/problemset/problem/2131/C

In this problem, using unordered_map in the same code will lead to a TLE, whereas replacing it with map in the exact same scenario will result in an Accepted verdict.

So always use a custom hash if you don't want to get blown up.

  • Vote: I like it
  • -6
  • Vote: I do not like it

»
9 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

You can refer to this blog for a better custom hash function