Skip to main content
๐Ÿ“œ WAYPOINT LESSON

C++ for Contests: the 10-Minute Bridge

โญ beginnerโณ 12 min read๐Ÿ“ Lesson 2 of 281

Types, I/O, vector, loops โ€” the exact C++ slice HSG problems need, plus fast I/O and the long long rule.

The minimal C++ for contests

This course assumes you know variables, loops, and functions from a first programming course (or you are taking cpp-beginner alongside). This is exactly the slice contest problems need.

Data types โ€” the golden rule

int a;        // up to ~2.1 * 10^9 โ€” LIMITED
long long x;  // up to ~9.2 * 10^18 โ€” THE CONTEST DEFAULT

On scratch paper: 10^9 + 10^9 = 2ยท10^9 overflows int. If a sum can pass 2.1ยท10^9, the whole computation must be long long. This is the #1 wrong-answer cause for beginners.

Fast input/output

ios_base::sync_with_stdio(false);
cin.tie(nullptr);

The two standard contest lines: disable C-stdio synchronization and the per-read flush. With million-number inputs the difference is dramatic.

vector โ€” every problem's array

int n; in >> n;
vector<long long> a(n);
for (long long& x : a) in >> x;   // read n elements
sort(a.begin(), a.end());          // STL sort (sorting module)

for (long long& x : a) โ€” the & makes it writable and avoids copies.

The solve convention

void solve(istream& in, ostream& out) {
    long long a, b;
    in >> a >> b;
    out << a + b << "\n";
}

All contest logic lives in solve; the system calls solve(cin, cout).

โšก Now practice

Ready to Code
Starter SetFour opening problems in real statement format: overflow-safe addition, max/boundary classification, tiering, and clock arithmetic with negative division.
6 challenges ยท ยท ~45 min