Course outline · 0% complete

0/27 lessons0%

Course overview →

Fast IO and the Contest Template

lesson 10-1 · ~10 min · 26/27

Quiz

Warm-up from lesson 9-1: freq[w]++ on a map where w is missing does what?

Making cin fast enough for big inputs

Judge problems can feed you hundreds of thousands of numbers. Two lines at the top of main make C++ streams read them fast:

std::ios::sync_with_stdio(false);  // stop syncing with C's stdio
std::cin.tie(nullptr);             // stop flushing cout before every cin

And prefer "\n" over std::endl (lesson 1-2): endl forces a flush on every line, which is slow in a tight loop.

Two more habits

Read until the input ends. std::cin >> x evaluates to false when input runs out, so an unknown-length stream is just:

int x;
while (std::cin >> x) { /* use x */ }

You used this in lesson 9-1's word counter.

Sum in long long. An int caps near 2.1 billion (lesson 2-1). Summing 100,000 values that can each be a billion overflows int silently. long long holds about ±9.2 × 10¹⁸. In contests, when in doubt, sums and products are long long.

Code exercise · cpp

Run the standard contest opening: fast IO, read-until-end, long long accumulator.

Quiz

Three int values of 1,000,000,000 are summed into an int. What happens?

Code exercise · cpp

Your turn. Read integers until input ends and print the smallest, the largest, and the sum (as long long). Track min and max as you read, as in lesson 3-3. Input is `4 -2 9 4 7`.