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.
| Verdict | Meaning |
|---|---|
| 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
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 to | Fast enough | Typical idea |
|---|---|---|
| 10 | O(n!) | try every permutation |
| 20 | O(2ⁿ) | try every subset |
| 500 | O(n³) | three nested loops, Floyd–Warshall |
| 5000 | O(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
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.