π C++ for Competitive Programming: A USACO Guide
From Zero to USACO Gold
The complete beginner's roadmap to competitive programming in C++, designed around USACO competition preparation.
No prior experience required. Written for clarity, depth, and contest readiness.
π― What Is This Book?
This book is a structured, self-contained course for students who want to learn competitive programming in C++ β specifically USACO (USA Computing Olympiad)
Unlike scattered online resources, this book gives you a single linear path: from writing your very first C++ program, through data structures and graph algorithms, all the way to solving USACO Gold problems with confidence. Every chapter builds on the previous one, with detailed worked examples, annotated C++ code, and SVG diagrams that make abstract algorithms visual and concrete.
If you've ever felt overwhelmed looking at USACO editorials, or if you know some programming but don't know what to learn next β this book was written for you.
β What You'll Learn
π Book Statistics
| Metric | Value |
|---|---|
| Parts / Chapters | 8 parts / 31 chapters |
| Code Examples | 150+ (all C++17, compilable) |
| Practice Problems | 130+ (labeled Easy/Medium/Hard) |
| SVG Diagrams | 55+ custom visualizations |
| Algorithm Templates | 20+ contest-ready templates |
| Appendices | 6 (Quick Ref, Problem Set, Tricks, Templates, Math, Debugging) |
| Estimated Completion | 8β12 weeks (1β2 chapters/week) |
| Target Level | USACO Bronze β Gold |
πΊοΈ Learning Path
π Quick Start (5 Minutes)
Step 1: Install C++ Compiler
Windows: Install MSYS2, then: pacman -S mingw-w64-x86_64-gcc
macOS: xcode-select --install in Terminal
Linux: sudo apt install g++ build-essential
Verify: g++ --version (should show version β₯ 9)
Step 2: Get an Editor
VS Code + C/C++ extension + Code Runner extension
Step 3: Competition Template
Copy this to template.cpp β use it as your starting point for every problem:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("problem.in", "r", stdin); // uncomment for file I/O
// freopen("problem.out", "w", stdout);
// Your solution here
return 0;
}
Step 4: Compile & Run
g++ -o sol solution.cpp -std=c++17 -O2 -Wall
./sol < input.txt
Step 5: Start Reading
Go to Chapter 2.1 and write your first C++ program. Then solve all practice problems before moving on. Don't skip the problems β that's where 80% of learning happens.
π How to Use This Book
The Reading Strategy That Works
- Read actively: Code every example yourself. Don't just read β type it out.
- Do the problems: Each chapter has 5β7 problems. Attempt every one before reading hints.
- Read hints when stuck (after 20β30 minutes of genuine effort)
- Review the Chapter Summary before moving on β it's a quick checklist.
- Return to earlier chapters when a later chapter references them.
Practice Problems Guide
Each practice problem is labeled:
- π’ Easy β Directly applies the chapter's main technique
- π‘ Medium β Requires combining ideas or a minor insight
- π΄ Hard β Challenging; partial credit counts!
- π Challenge β Beyond chapter scope; try when ready
All hints are hidden by default (click to expand). Struggle first!
Reading Schedule
| Stage | Chapters | Recommended Time |
|---|---|---|
| Foundations | 2.1β2.3 | 1β2 weeks |
| Data Structures | 3.1β3.11 | 2β3 weeks |
| Greedy | 4.1β4.2 | 1 week |
| Graphs | 5.1β5.4 | 2β3 weeks |
| DP | 6.1β6.3 | 3β4 weeks |
| USACO Contest Guide | 7.1β7.3 | 1 week |
| USACO Gold | 8.1β8.5 | 3β4 weeks |
π Chapter Overview
Part 2: C++ Foundations (1β2 weeks)
| Chapter | Topic | Key Skills |
|---|---|---|
| Ch.2.1: First C++ Program | Hello World, variables, I/O | cin, cout, int, long long |
| Ch.2.2: Control Flow | Conditions and loops | if/else, for, while, break |
| Ch.2.3: Functions & Arrays | Reusable code, collections | Arrays, vectors, recursion |
Part 3: Core Data Structures (2β3 weeks)
| Chapter | Topic | Key Skills |
|---|---|---|
| Ch.3.1: STL Essentials | Powerful built-in containers | sort, map, set, stack, queue |
| Ch.3.2: Arrays & Prefix Sums | Range queries inO(1) | 1D/2D prefix sums, difference arrays |
| Ch.3.3: Sorting & Searching | Efficient ordering and lookup | sort, binary search, BS on answer |
| Ch.3.4: Two Pointers & Sliding Window | Linear-time array techniques | Two pointer, fixed/variable windows |
| Ch.3.5: Monotonic Stack & Monotonic Queue | Monotonic data structures | Next greater element, sliding window max |
| Ch.3.6: Stacks, Queues & Deques | Order-based data structures | stack, queue, deque; LIFO/FIFO patterns |
| Ch.3.7: Hashing Techniques | Fast key lookup and collision handling | unordered_map/set, polynomial hashing, rolling hash |
| Ch.3.8: Maps & Sets | Key-value lookup and uniqueness | map, set, multiset |
| Ch.3.9: Introduction to Segment Trees | Range queries with updates | Segment tree build/query/update |
| Ch.3.10: Fenwick Tree (BIT) | Efficient prefix-sum with point updates | Binary Indexed Tree, BIT update/query, inversion count |
| Ch.3.11: Binary Trees | Tree data structure fundamentals | Traversals, BST operations, balanced trees |
Part 4: Greedy Algorithms (1 week)
| Chapter | Topic | Key Skills |
|---|---|---|
| Ch.4.1: Greedy Fundamentals | When greedy works (and fails) | Activity selection, exchange argument |
| Ch.4.2: Greedy in USACO | Contest-focused greedy | Scheduling, binary search + greedy |
Part 5: Graph Algorithms (2β3 weeks)
| Chapter | Topic | Key Skills |
|---|---|---|
| Ch.5.1: Introduction to Graphs | Modeling relationships | Adjacency list, graph types |
| Ch.5.2: BFS & DFS | Graph traversal | Shortest path, multi-source BFS, cycle detection, topo sort |
| Ch.5.3: Trees & Special Graphs | Tree algorithms | DSU, Kruskal's MST, tree diameter, LCA, Euler tour |
| Ch.5.4: Shortest Paths | Weighted graph shortest paths | Dijkstra, Bellman-Ford, Floyd-Warshall |
Part 6: Dynamic Programming (3β4 weeks)
| Chapter | Topic | Key Skills |
|---|---|---|
| Ch.6.1: Introduction to DP | Memoization and tabulation | Fibonacci, coin change |
| Ch.6.2: Classic DP Problems | Core DP patterns | LIS, 0/1 Knapsack, grid paths |
| Ch.6.3: Advanced DP Patterns | Harder techniques | Bitmask DP, interval DP, tree DP, digit DP |
Part 7: USACO Contest Guide (Read anytime)
| Chapter | Topic | Key Skills |
|---|---|---|
| Ch.7.1: Understanding USACO | Format, divisions, scoring, problem taxonomy | Contest strategy, upsolving, pattern recognition |
| Ch.7.2: Problem-Solving Strategies | How to think about problems | Algorithm selection, debugging |
| Ch.7.3: Ad Hoc Problems | Observation-based problems with no standard algorithm | Invariants, parity, cycle detection, constructive thinking |
Part 8: USACO Gold Topics (4 weeks)
| Chapter | Topic | Key Skills |
|---|---|---|
| Ch.8.1: Minimum Spanning Tree | Connect all nodes with minimum edge cost | Kruskal (DSU), Prim (priority queue), cut/cycle properties |
| Ch.8.2: Topological Sort & DAG DP | Ordering in directed acyclic graphs | Kahn's algorithm, DFS toposort, longest path, counting paths |
| Ch.8.3: Tree DP & Rerooting | DP on trees; rerooting technique | Subtree DP, sum of distances, max independent set on tree |
| Ch.8.4: Euler Tour & Tree Flattening | Flatten tree to array for range queries | DFS timestamps, subtree queries, binary lifting, LCA |
| Ch.8.5: Combinatorics & Number Theory | Counting and number properties | Modular inverse, C(n,k) mod p, inclusion-exclusion, sieve |
Appendix & Reference
| Section | Content |
|---|---|
| Appendix A: C++ Quick Reference | STL cheat sheet, complexity table |
| Appendix B: USACO Problem Set | Curated problem list by topic and difficulty |
| Appendix C: Competitive Programming Tricks | Fast I/O, macros, modular arithmetic |
| Appendix D: Contest-Ready Templates | DSU, Segment Tree, BFS, Dijkstra, binary search, modpow |
| Appendix E: Math Foundations | Modular arithmetic, combinatorics, number theory, probability |
| Appendix F: Debugging Guide | Common bugs, debugging techniques, AddressSanitizer |
| Glossary | 35+ competitive programming terms defined |
| π Knowledge Map | Interactive chapter dependency graph β click nodes to explore prerequisites |
π§ Setup Instructions
Compiler Setup
| Platform | Command |
|---|---|
| Windows (MSYS2) | pacman -S mingw-w64-x86_64-gcc |
| macOS | xcode-select --install |
| Linux (Debian/Ubuntu) | sudo apt install g++ build-essential |
Verify with: g++ --version
Recommended Compile Flags
# Development (shows warnings, helpful for debugging)
g++ -o sol solution.cpp -std=c++17 -O2 -Wall -Wextra
# Contest (fast, silent)
g++ -o sol solution.cpp -std=c++17 -O2
Running with I/O Redirection
# Run with input file
./sol < input.txt
# Run and save output
./sol < input.txt > output.txt
# Compare output to expected
diff output.txt expected.txt
π Local Development / Build This Book
This project uses mdBook (a Rust-based book builder) as its build system. GitBook is no longer used.
Prerequisites: Install mdBook
Via Homebrew (macOS):
brew install mdbook
Via Cargo (cross-platform, requires Rust):
cargo install mdbook
Via pre-built binary: Download from mdBook GitHub Releases
Verify installation: mdbook --version
Local Preview (with hot-reload)
mdbook serve
This starts a local server at http://localhost:3000 with live-reload. Edit any .md file and the browser will automatically refresh.
Local Build
mdbook build
Build output is in the book/ directory. Open book/index.html in a browser to view the static site.
CI/CD
The project uses GitHub Actions to automatically build and deploy via mdBook. See .github/workflows/deploy.yml for details.
π External Resources
| Resource | What It's Best For |
|---|---|
| usaco.org | Official USACO problems + editorials |
| usaco.guide | Community guide, curated problems by topic |
| codeforces.com | Additional practice problems, contests |
| cp-algorithms.com | Deep dives into specific algorithms |
| atcoder.jp | High-quality educational problems (AtCoder Beginner) |
π Who Is This Book For?
β Middle school / high school students preparing for USACO Bronze through Gold
β Complete beginners with no prior programming experience (Part 2 starts from zero)
β Intermediate programmers who know Python or Java and want to learn C++ for competitive programming
β Self-learners who want a structured, complete curriculum instead of scattered tutorials
β Coaches and teachers looking for a comprehensive curriculum for their students
This book is NOT for:
- USACO Gold/Platinum (advanced data structures, network flow, geometry)
- General software engineering (no databases, web development, etc.)
π Ready? Let's Begin!
Turn to Chapter 2.1 and write your first C++ program.
The path from complete beginner to USACO Gold is roughly 300β500 hours of focused practice over 3β8 months. It won't always be easy β but every USACO Gold competitor you admire started exactly where you are now.
The only way to get better is to write code, struggle with problems, and keep going. π
Last updated: 2026 Β· Targets: USACO Bronze β Gold Β· C++ Standard: C++17 55+ SVG diagrams Β· 150+ code examples Β· 130+ practice problems