Contests, judges and time limits

In a programming contest (ICPC, Codeforces, the national olympiad) you solve problems by writing programs. An online judge compiles your code, runs it on hidden tests and compares its output with the right answers, exactly like the practice problems on StepVioz.

VerdictMeaning
Accepted (AC)right output on every test, within the limits
Wrong Answer (WA)some output differs
Time Limit Exceeded (TLE)too slow (usually 1 to 2 seconds)
Runtime Error (RE)a crash: bad index, division by zero, stack overflow
Memory Limit Exceeded (MLE)too much memory

Reading input quickly

#include <iostream>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long x, sum = 0;
    int count = 0;
    while (cin >> x) {
        sum += x;
        count++;
    }
    cout << count << " numbers, sum " << sum << "\n";
    return 0;
}

Input used: 5 8 -2 ⏎ 10 4

Open in compiler

Contest C++ usually starts with ios::sync_with_stdio(false); cin.tie(nullptr);, which makes cin and cout much faster on big inputs. Use "\n" rather than endl, which also flushes the output each time. while (cin >> x) reads until the input ends, for problems that do not say how many numbers come.

Is my idea fast enough?

A judge machine runs roughly 10⁸ simple operations per second. Look at the limits on n and pick an algorithm that fits:

n up toFast enoughTypical idea
10O(n!)try every permutation
20O(2ⁿ)try every subset
500O(n³)three nested loops, Floyd–Warshall
5000O(n²)two nested loops, simple DP
10⁶O(n log n)sorting, binary search, heaps
10⁸O(n)one pass

Contest habits. Read the whole statement and the limits first. Test the samples and the smallest and largest cases before you submit. Use long long whenever a sum or product can pass about 2 × 10⁹.

Quick check

1. n can be 200,000. Is an O(n²) solution fast enough?
2. What does Wrong Answer mean?
3. Why use "\n" instead of endl in contests?

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.