Introduction to C++ · Lesson 3 of 33
Fast Input/Output and Contest Templates in C++
In coding contests and online assessments, a correct solution can still fail with "Time Limit Exceeded" just because reading a million numbers is slow. Two lines fix that. This lesson shows how contest input is structured, how to read it fast and safely, and a clean template you can start every problem with.
In this lesson
- Fast I/O with
sync_with_stdioandcin.tie - Reading multiple test cases
- Using the right integer types
- A contest template, and
bits/stdc++.h
The two magic lines
#include <iostream>
int main() {
std::ios::sync_with_stdio(false); // don't keep cin/cout in sync with C's scanf/printf
std::cin.tie(nullptr); // don't flush cout before every cin
long long sum = 0;
int n;
std::cin >> n;
for (int i = 0; i < n; i++) {
long long x;
std::cin >> x;
sum += x;
}
std::cout << sum << '\n';
return 0;
}
3000000004
After sync_with_stdio(false)
Don't mix cin/cout with scanf/printf in the same program – their outputs can appear out of order. Pick one style.
Contest input usually has no prompts (the judge sends numbers, not a person), so the output panel above shows only the answer. Notice long long: three billions don't fit in an int (max ≈ 2.1 × 10⁹). Overflow is the most common wrong-answer cause in contests.
Multiple test cases
#include <iostream>
#include <vector>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t; // number of test cases
while (t--) {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int& x : a) std::cin >> x; // read straight into the vector
int best = a[0];
for (int x : a) best = std::max(best, x);
std::cout << best << '\n';
}
return 0;
}
9 -5 10
A typical problem's input format
- tthe number of test cases
- for each test: nthe size of this test
- n valuesread into a vector of size n
- print one answer per testeach on its own line
Reading until the input ends
#include <iostream>
int main() {
int x, count = 0, total = 0;
while (std::cin >> x) { // stops at end of input or a non-number
total += x;
count++;
}
std::cout << count << " numbers, total " << total << '\n';
return 0;
}
6 numbers, total 108
A contest template
#include <bits/stdc++.h> // GCC only: includes the whole standard library
using namespace std;
using ll = long long;
#define all(v) (v).begin(), (v).end()
void solve() {
int n;
cin >> n;
vector<ll> a(n);
for (auto& x : a) cin >> x;
sort(all(a));
cout << a.back() - a.front() << '\n'; // e.g. the range of the values
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--) solve();
}
| Shortcut | Why contestants use it | Caution |
|---|---|---|
#include <bits/stdc++.h> | one include for everything | GCC-only (not Clang/MSVC), slower to compile – never in real projects |
using namespace std; | less typing | fine in a one-file solution; never in headers |
using ll = long long; | short name for 64-bit integers | prefer using over #define for types |
solve() per test | clean state for every test case | reset globals if you use any |
Interview vs contest code
In a timed contest, short code wins. In an interview or a job, readable code wins: proper includes, std::, clear names and functions. Know both styles – this course uses the readable one and shows contest style where it helps.
Practice
Try it yourself
For each test case, read n numbers and print how many are even.
Show solution
#include <iostream> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { int n, even = 0; std::cin >> n; for (int i = 0; i < n; i++) { int x; std::cin >> x; if (x % 2 == 0) even++; } std::cout << even << '\n'; } }3 0
Quick check
What does cin.tie(nullptr) do?
Stops cout being flushed before each cin read, which speeds up programs that mix input and output.
When should you use long long?
Whenever values or sums can exceed about 2 × 10⁹.
Is bits/stdc++.h standard C++?
No – it's a GCC-specific header; avoid it outside contests.
Summary
- Add
sync_with_stdio(false)andcin.tie(nullptr)for fast I/O; don't mix with printf. - Read t, then each test; read into vectors; use long long for big sums.
- A solve() template keeps contest code clean; keep shortcuts out of real projects.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.