Overview
The standard template provides a minimal setup for competitive programming problems with fast I/O and basic structure.
Basic Template
Simple template for single-problem contests.
/**
* @created : [Date]
* @handle : @YourHandle
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// Your code here
return 0;
}
Fast I/O Setup
ios_base::sync_with_stdio(0)
Disables synchronization between C and C++ I/O streams for faster input/output
Unties cin from cout, allowing buffered input without flushing output
Performance Impact: Can provide 2-3x speedup for I/O-heavy problems
Template with Test Cases
For problems with multiple test cases.
/**
* @created : [Date]
* @handle : @YourHandle
*/
#include <bits/stdc++.h>
using namespace std;
void solve() {
// Solve single test case
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
Comprehensive header with common includes and type aliases.
#include <bits/stdc++.h>
using namespace std;
// Type aliases
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
// Constants
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LLINF = 1e18;
// Macros
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
// Debug
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " = " << (x) << endl
#else
#define debug(x) 42
#endif
Class-Based Template
Structured approach with separate read/solve methods.
/**
* @created : [Date]
* @handle : @YourHandle
*/
#include <bits/stdc++.h>
using namespace std;
class Task {
private:
// Variables
void clear() {
// Clear variables for next test case
}
void read() {
// Read input
}
void solveOne() {
// Solve single test case
}
public:
void solve() {
int T = 1;
cin >> T;
while (T-- > 0) {
clear();
read();
solveOne();
}
}
private:
void print() { cout << '\n'; }
template <class H, class... T>
void print(const H &h, const T &...t) {
cout << h;
if (sizeof...(t))
cout << ' ';
print(t...);
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Task solver;
solver.solve();
cout.flush();
return 0;
}
Benefits of Class-Based Approach
Organization
Clear separation between input, processing, and output
State Management
Easy to clear state between test cases
Debugging
Isolated methods are easier to debug
Reusability
Methods can be reused across problems
Random Testing Template
Template for generating random test cases.
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
void generate_test() {
int n = random(1, 100);
cout << n << endl;
for(int i = 0; i < n; i++) {
cout << random(1, 1000) << " ";
}
cout << endl;
}
int main() {
int num_tests = 100;
for(int test = 1; test <= num_tests; test++) {
cout << "Test " << test << ":" << endl;
generate_test();
}
return 0;
}
Common Template Additions
template<typename T>
void read(vector<T> &v) {
for(T &x : v) cin >> x;
}
template<typename T>
void print(const vector<T> &v) {
for(const T &x : v) cout << x << " ";
cout << endl;
}
inline int readInt() {
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
vector<string> grid(n);
for(int i = 0; i < n; i++) {
cin >> grid[i];
}
// Or for char grid
vector<vector<char>> grid(n, vector<char>(m));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
File I/O
For problems requiring file input/output.
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// Your code here
return 0;
}
Available Templates
| Template | File | Best For |
|---|
| Standard | template_std.cpp | Single problems |
| Test Cases | template_cases.cpp | Multiple test cases |
| Header | template_header.cpp | Common definitions |
| Random | template_random.cpp | Stress testing |
Best Practices
Fast I/O
Always use fast I/O for time-critical problems
Clear State
Remember to clear global variables between test cases
Test Locally
Test with sample inputs before submitting
Debug Mode
Use conditional debug that’s disabled on online judges
Common Mistakes to Avoid
- Forgetting to clear global arrays between test cases
- Not using long long when values exceed 2×10⁹
- Mixing cin/cout with scanf/printf after disabling sync
- Not flushing output with
cout.flush() for interactive problems
- Using endl instead of ‘\n’ (endl flushes buffer, slower)
See Also
Contest Templates
Templates for USACO, Google Code Jam, and LeetCode
Debugging
Debugging utilities and tools