Competitive programming

Stage 1 · Lesson 3 of 7

Your template and fast input and output

C++ for contests

A template is the short file you start every problem from: one header that includes the whole standard library, a long long alias, two lines for , and a loop over test cases. Fast IO matters because reading 10^6 numbers with default cin settings can be slow enough to exceed the .

Printing a newline character instead of endl matters too, since endl flushes the output every line. Many problems start with t test cases, so the template reads t and solves each one. Keep it short enough to type from memory, because at an site you may start from a blank editor.

Common trapcopying a long template full of macros from a strong contestant and not understanding half of it.

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?