Stage 1 · Lesson 8 of 8
Hash maps and anti-hash tests
unordered_map is a with average O(1) operations, often faster than map. On , though, other contestants can your solution: submit a test designed to break it. With the default hash function, a test can make every key land in the same bucket, and each operation becomes O(n), turning an solution into after the round.
A custom hash with a random seed, such as the splitmix64 function, defends against this, and so does simply using map. Know both options and when the extra speed is worth it.
Common trapassuming unordered_map is always the faster choice, when for small inputs or hostile tests it can be slower.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CourseFreeLearnCpp.com
LearnCpp.com · Beginner
Free and thorough: read the chapters on vectors, references, lambdas and the standard library if C++ is new to you
- BookFreeCompetitive Programmer's Handbook
Antti Laaksonen · Beginner
Chapter 1 covers C++ for contests, input and output and working with numbers; Chapter 2 covers complexity
Practise
Keep handy
Also worth your time
A clear walk through time and space complexity, the language you need to read constraints
- USACO GuideGo deeper
The General section has practical pages on choosing a language, fast input and output and debugging