For problems with or more numbers, input and output can dominate the runtime. The fixes are short and ordered by how much they buy you.
Step 1 β unsync the streams
ios::sync_with_stdio(false);
cin.tie(nullptr);C++ streams are, by default, kept synchronised with Cβs stdio so that printf and cout interleave correctly. Detaching them lets cin/cout use their own buffers and is typically a 3-5x speedup.
| Line | Effect |
|---|---|
sync_with_stdio(false) | stops mirroring every operation into stdio |
cin.tie(nullptr) | stops flushing cout before every cin read |
Two consequences
After unsyncing, do not mix
cin/coutwithscanf/printfβ the interleaving is no longer guaranteed. And never usecin.tie(nullptr)in an interactive problem.
Step 2 β stop using endl
cout << x << '\n'; // correct
cout << x << endl; // flushes the buffer every single timeendl is '\n' plus a flush. Printing lines with endl performs a million flushes and can cost a full second on its own. This one character is the most frequent cause of βmy solution got TLEβ.
Step 3 β build the output in a string
string out;
for (int i = 0; i < n; i++) { out += to_string(ans[i]); out += '\n'; }
cout << out;One write instead of . Worth doing when the output is large; unnecessary otherwise.
Step 4 β a hand-rolled reader
Only when the above is measurably insufficient β typically integers.
static char buf[1 << 25];
static size_t pos = 0, len = 0;
inline char gc() {
if (pos == len) { len = fread(buf, 1, sizeof buf, stdin); pos = 0; if (!len) return EOF; }
return buf[pos++];
}
int readInt() {
int c = gc();
while (c != '-' && (c < '0' || c > '9')) c = gc();
int sgn = 1;
if (c == '-') { sgn = -1; c = gc(); }
int x = 0;
while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = gc(); }
return x * sgn;
}Reading a whole block with fread and parsing it by hand skips all stream machinery. Expect another 2-3x over unsynced cin.
The matching writer:
inline void writeInt(long long x) {
static char tmp[24];
if (x < 0) { putchar_unlocked('-'); x = -x; }
int n = 0;
do { tmp[n++] = '0' + x % 10; x /= 10; } while (x);
while (n--) putchar_unlocked(tmp[n]);
}putchar_unlocked skips the per-call mutex putchar acquires β safe in a single-threaded program, and roughly twice as fast.
Reading strings and lines
string s;
cin >> s; // stops at whitespace
getline(cin, s); // reads a whole line
cin >> n; cin.ignore(); getline(cin, s); // the newline after n must be consumedForgetting the cin.ignore() after a numeric read is a standard bug: getline immediately returns the empty remainder of the previous line.
Reading until EOF
Common on Kattis and older judges:
int x;
while (cin >> x) { ... }What to expect
| Method | Rough time for integers |
|---|---|
cin with sync on | ~4 s |
cin unsynced | ~0.9 s |
scanf | ~1.2 s |
fread + hand parser | ~0.15 s |
Numbers vary by judge, but the ordering does not.
Measure before optimising
If the solution is on , no amount of I/O tuning saves it. Check the complexity first, then the algorithmic constant (Constant Factor), and only then the I/O. Reading input is rarely the bottleneck below values.
See also: Contest Template Β· Constant Factor Optimization Β· C++ Tips