Competitive programming

Stage 1 · Lesson 5 of 7

Sets, maps and priority queues

C++ for contests

set and map keep elements sorted in a balanced tree, so inserting, erasing and finding cost O(log n). lower_bound on a set finds the first element not below a value, which answers "what is the next larger item" while the data changes.

multiset allows duplicates, and map counts or groups things by key. priority_queue is a : it always gives the largest element (or the smallest, with greater), which is what and many scheduling greedies need. Knowing which container answers which question quickly is half of many easier problems.

Common traperasing a value from a with erase(x), which removes every copy instead of one.

A question to check yourself, an AI practice session and 2 cheat sheet entries 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?