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

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

Can someone help me why unordered_map is out of control in Problem C?

Failed in test case 4 with GNU C++17 submission

Failed in test case 5 with GNU C++14 submission

Passed with Clang++17 Diagnostics submission

Finally passed by changing unordered_map to map submission

I wasted 1hr debugging this during the contest!!

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

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

When acessing m[k], map (and unordered_map) always puts m[k] = default inside map if there is no such key, so doing this inside cycle isn't a good idea, because you may iterate over map in a wrong order (and pass some elements). Then you use map it's okay cause you are looking only at elements with positive keys and adds elements with negative keys, so order stays the same cause all elements are sorted by key. But unordered_map is... unordered, so it does not work.

Common fix is to use m.count(k); That way you can check whether map contain element with given key without adding this key to map. submission