In competitive programming, I/O can be the bottleneck. A problem with 10^6 integers using cin/cout without optimization will TLE, while the same code with fast I/O passes easily.
The I/O Bottleneck: Standard cin/cout is synchronized with C’s scanf/printf by default, making it significantly slower. Disable this sync to gain 5-10x speedup.
Every competitive programmer has a template. Here’s a solid starting point:
Copy
#include <bits/stdc++.h>using namespace std;// Type aliases for faster typingusing ll = long long;using ld = long double;using pii = pair<int, int>;using pll = pair<ll, ll>;using vi = vector<int>;using vll = vector<ll>;using vvi = vector<vi>;using vpii = vector<pii>;// Macros for common operations#define all(x) (x).begin(), (x).end()#define sz(x) (int)(x).size()#define pb push_back#define ff first#define ss second#define rep(i, a, b) for (int i = (a); i < (b); i++)#define per(i, a, b) for (int i = (b) - 1; i >= (a); i--)// Constantsconst int MOD = 1e9 + 7;const int INF = 1e9;const ll LINF = 1e18;void solve() { // Your solution here}int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; cin >> t; // Comment this for single test case while (t--) { solve(); } return 0;}
int main() { // MUST be at the start of main ios::sync_with_stdio(false); // Disable sync with C I/O cin.tie(nullptr); // Untie cin from cout // Now cin/cout are as fast as scanf/printf}
Important: After ios::sync_with_stdio(false), do NOT mix cin/cout with scanf/printf in the same program.
// Reading a line with spacesstring line;getline(cin, line);// After reading a number, clear the newline before getlineint n;cin >> n;cin.ignore(); // or getline(cin, line); to consume newlinegetline(cin, line);
// Read until EOFint x;while (cin >> x) { // Process x}// Read n integers into vectorint n;cin >> n;vector<int> a(n);for (int& x : a) cin >> x;// Or using istream_iterator (fancy but slower)vector<int> a(istream_iterator<int>(cin), istream_iterator<int>());
// Use '\n' instead of endl (endl flushes buffer)cout << ans << '\n'; // Fastcout << ans << endl; // Slow - forces buffer flush// For many outputs, build string then print oncestring result;for (int i = 0; i < n; i++) { result += to_string(arr[i]) + " ";}cout << result << '\n';
// n nodes, m edgesint n, m;cin >> n >> m;// Adjacency listvector<vector<int>> adj(n + 1); // 1-indexedfor (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); // For undirected graph}// Weighted edgesvector<vector<pair<int, int>>> adj(n + 1);for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({v, w}); adj[v].push_back({u, w});}
// WRONG: Overflow when n > 46340int n = 100000;int result = n * n; // Overflow!// CORRECT: Cast to long longlong long result = (long long)n * n;// Or use ll from the startll n = 100000;ll result = n * n;
// WRONG: Accessing arr[n] when array has n elementsvector<int> arr(n);cout << arr[n]; // Undefined behavior!// CORRECT: Valid indices are 0 to n-1cout << arr[n-1];
// WRONG: Global variables not reset between test casesint visited[100001]; // Globalvoid solve() { // visited still has values from previous test case!}// CORRECT: Reset at start of solve()void solve() { memset(visited, 0, sizeof(visited)); // or use local variables vector<int> visited(n + 1, 0);}
// Default precision may not be enoughdouble ans = 1.0 / 3.0;cout << ans << '\n'; // 0.333333// Set precisioncout << fixed << setprecision(9) << ans << '\n'; // 0.333333333
When your solution gets WA and you can’t find the bug:
Copy
// generate.cpp - Generates random test cases#include <bits/stdc++.h>using namespace std;int main(int argc, char* argv[]) { srand(atoi(argv[1])); // Seed from command line int n = rand() % 10 + 1; // Small n for testing cout << n << '\n'; for (int i = 0; i < n; i++) { cout << rand() % 100 << ' '; } cout << '\n';}
Copy
# stress.sh - Run until mismatchfor ((i = 1; ; i++)); do ./generate $i > input.txt ./solution < input.txt > output1.txt ./brute < input.txt > output2.txt if ! diff -q output1.txt output2.txt > /dev/null; then echo "Mismatch found on test $i!" cat input.txt break fi echo "Test $i passed"done