Welcome to the world of Competitive Programming (CP).
Here, coding is no longer just a tool for building software or websites—it becomes a weapon for your thinking. Within the limits of time, you use code to solve tightly defined math or simulation problems. This sharply improves your logic in addition to helping you earn offers from top companies.
1.1 What is Competitive Programming?
Simply put, it is a mental sport. A contest usually gives you three to ten problems; you write programs to read the provided input, perform calculations, and print the required output.
Although contest formats vary, they mostly fall into three camps, and you can choose where to focus based on your background:
- Olympiad in Informatics (OI) track
- Representative contests: NOIP, CSP, IOI.
- Typical participants: high school and middle school students.
- Features: Sessions last 3–5 hours, partial scoring is common, and they grade how many test cases you pass rather than just AC/WA.
- ICPC-style contests (Collegiate Programming Contest)
- Representative contests: ICPC, CCPC.
- Typical participants: university students.
- Features: Teams of three share one computer. Rankings are based on solved count plus penalty time. Each problem is either accepted or wrong, which makes the race thrilling.
- Online platforms
- Representative platforms: Codeforces, LeetCode (weekly contests), AtCoder.
- Typical participants: open to everyone.
- Features: Rating systems track your strength; these are the best arenas for daily practice and proof of skill.
1.2 Common Online Judges (OJ)
An OJ is an online judge. You submit code, and the server runs it and returns the verdict. For newcomers, focus on these three platforms:
| Platform | URL | Notes | Recommended use |
|---|---|---|---|
| Luogu | https://www.google.com/search?q=luogu.com.cn | The most active Chinese community, massive problem set, detailed editorials. | Best for beginners, especially NOIP-level and entry-level problems. |
| LeetCode | leetcode.cn | Problem statements resemble engineering interviews; difficulty is moderate. | Great for job seekers building confidence for interviews. |
| Codeforces | codeforces.com | The top global competitive programming platform, created in Russia. | Essential for leveling up quickly—its contests boost mental speed. |
Tip: Sign up on Luogu now. Most examples in this series will reference its problem set.
1.3 How Judging Works: Inside the "Black Box"
This is what trips beginners up. After you hit submit, the OJ throws your code into a black box.
The process follows the Standard I/O workflow:
- Input: Your program reads from
stdin(keyboard/console). - Processing: The program runs within the time limit (e.g., 1 second) and memory limit (e.g., 256MB).
- Output: The result is printed to
stdout(screen/console). - Comparison: The OJ compares your output with the official answer character by character.
Common Verdicts
Seeing a green Accepted is every competitor's dopamine hit. Before that happens, you will see:
- AC (Accepted) ✅: Congratulations! Your output exactly matches the judge's answer.
- WA (Wrong Answer) ❌: Your logic is wrong, or you missed edge cases (e.g., input is 0 or a negative number).
- TLE (Time Limit Exceeded) ⏳: The algorithm is too slow; it exceeds the time limit (usually 1 second).
- MLE (Memory Limit Exceeded) 💾: You allocated more memory than allowed.
- RE (Runtime Error) 💥: The program crashed, commonly due to division by zero, out-of-bounds access, or deep recursion.
- CE (Compilation Error) 🚧: There are syntax errors, so the judge can't even build your code.
1.4 The Competitive Programmer's Mindset
Engineers usually write code for maintainability, documentation, and architecture. Competitors focus on abstraction and efficiency.
Example:
- Real problem: Connect N villages with roads so that the total length is as short as possible.
- Algorithmic abstraction: This becomes a graph problem—find the minimum spanning tree (MST) in a weighted undirected graph.
Three habits you must cultivate:
- Check the input size: The first thing you do is look at N. If
N <= 1000,O(N^2)might pass; ifN <= 10^5, you must aim forO(N log N). - Consider corner cases: What if the input is empty? Or the maximum representable value? Or negative?
- Ditch the mouse: Many contests provide Linux terminals. Practice coding entirely from the keyboard.
1.5 Your First Challenge: The A+B Problem
This is the "Hello World" of algorithms.
Problem: Read two integers a and b, print their sum.
C++ reference template:
#include <iostream> // input/output stream
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) {
cout << a + b << endl;
}
return 0;
}Why the while loop? Some judges feed multiple test cases. while (cin >> a >> b) means you keep processing pairs as long as they are available, stopping at EOF. This is very common in contests.
🚀 Next
You now understand the rules. The next step is to gear up your environment and start coding.