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

Автор prthptl14, история, 5 лет назад, По-английски

Doubt When should I use unordered_map and when should I use a map in c++.In this question Div 3 #479 contest question F when I submit using unordered_map I got TLE but the same solution when I submit using a map my code accepted. Here is my solution using unordered_map and using map. Please help me.

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

»
5 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

It's because of you use

#define int long long 

in the first code but don't use it in the second one

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

I just modified your code a little bit:

AC (140ms) after using custom hash: 80916930. Learn more here.

AC (124ms) after further optimization: 80917090. Learn more here.

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

Since unordered_map is not sorted as elements are stored in hash tables, you use that when u want to access the single element i.e no traversal is required or store the frequency of element and u don't want it sorted.

Whereas ordered_map or simply map is sorted as elements are stored in BST, it is used when u want elements in a sorted manner, when u want to traverse in order. Or u want to find(search) an element as it uses the concept of binary search O(log n). U can also use the concept of lower_bound and upper_bound in this.