Competitive programming

Stage 1 · Lesson 8 of 8

Hash maps and anti-hash tests

C++ for contests

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

  • CourseFree
    LearnCpp.com

    LearnCpp.com · Beginner

    Free and thorough: read the chapters on vectors, references, lambdas and the standard library if C++ is new to you

  • BookFree
    Competitive Programmer's Handbook

    Antti Laaksonen · Beginner

    Chapter 1 covers C++ for contests, input and output and working with numbers; Chapter 2 covers complexity

Practise

  • Start with the Introductory Problems section: short tasks that exercise loops, strings and the STL

  • Filter the problem set to rating 800 and solve a few each day to get used to contest input and output

Keep handy

  • Keep it open for exact signatures and complexities of vector, set, map, sort and lower_bound

Also worth your time

Mark this stage as not done?