Part 6: Codeforces patterns¶
This part covers the techniques that short Codeforces problems keep using: fast input and output, counting steps, two pointers, prefix sums, binary search, number theory, sliding windows, and a first set of problems worked through in full. Work through the lessons in order, because each one uses the ideas of the lessons before it.
- Fast input and output template: Read all input at once, split it into tokens, and write the answer in one call. ~15 min
- Counting steps and choosing a Python interpreter: Count loop iterations as formulas, estimate whether they fit a time limit, and learn what PyPy is. ~15 min
- Two pointers on sorted data: Move two indices through a sorted list, and show that the scan takes at most
n - 1steps. ~20 min - Prefix sums and difference arrays: Answer range sums in constant time and apply many range updates in one pass. ~20 min
- Binary search and the bisect module: Find positions with
bisect, and search for the smallest or largest answer that passes a test. ~20 min - Number theory: gcd, divisors, sieve, and modular powers: Compute gcd and lcm, list divisors, sieve primes, and use
powwith a modulus. ~25 min - Sliding window over a list: Keep a moving window of a list, and learn when the shrink rule is valid. ~20 min
- Ten first problems, worked through: Solve ten short problems in the 800 to 1200 range, with pitfalls and tested solutions. ~45 min