rating_Infi's blog

By rating_Infi, history, 5 years ago, In English

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!!

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

| Write comment?
»
5 years ago, # |
Rev. 3   Vote: I like it +1 Vote: I do not like it

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