DON'T CODE YET!
By M.Rezaee
#include <bits/stdc++.h>
using namespace std;
int main() {
Codeforces problem;
if (!problem.read()) {
return 0;
}
// Step 1: Check the constraints.
problem.check_constraints();
// Step 2: Try the simplest idea.
auto brute = problem.brute_force();
// Step 3: Find the observation.
while (!problem.has_key_observation()) {
problem.ask:
"Can I reorder something?",
"Can I solve it backwards?",
"What happens in small cases?",
"Is something invariant?",
"Can I simplify the process?";
}
// Step 4: Only now, write the solution.
if (problem.has_clear_idea()) {
problem.code();
problem.test();
} else {
cout << "DON'T CODE YET!" << '\n';
}
}
I think one of the most common mistakes in competitive programming is coding before understanding the problem.
You read the statement, look at the examples, and immediately start typing.
Then 20 minutes later:
WA
WA
TLE
...
What am I even doing?
Before coding, ask yourself three simple things:
1. What do the constraints allow?
n <= 20 and n <= 2 * 10^5 are completely different worlds.
2. What is the simplest possible solution?
Even if it is too slow, a brute-force solution can help you discover what needs to be optimized.
3. What is the key observation?
Sometimes the entire problem becomes easy after one small observation.
Try small cases. Reverse the process. Reorder things. Look for invariants. Question the statement.
The goal isn't to think for 30 minutes before every problem.
It's simply to avoid writing 100 lines of code when you don't even know what those 100 lines are supposed to do.
So before you touch the keyboard, ask yourself:
"Do I actually know what I'm going to code?"
If the answer is no...







