View on GitHub

competitive-programming-cheatsheet

Competitive programming cheatsheet

This is a cheat sheet made for when you are stuck on a problem and can’t find the optimal solution. This will refresh your memory and hopefully help you find the concept/approach you should be using for your problem.

This is not made to teach your any of these concepts/approaches, it’s only made to help you quickly identify which one you should be using for each problem.

If you’d like to start in Competitive Programming, I’d recommend reading the Competitive Programmer’s Handbook by Antti Laaksonen. Most, if not all of the things mentioned here are explained in detail in this book.

If you aren’t familiar with one of these concepts/approaches I’d recommend you learn it at Usaco Guide. That is also the source for most of the example problems.

Note: time complexity and space complexity are very dependent on the input data and therefore the ones written here are purely an estimation of what they usually are in common problems.

Approaches/Concepts

Sorting

Greedy algorithms

Prefix sum

Dynamic programming

Sliding window

If none from these approaches will do it for your problem, maybe using the right data type will. Usually these data types will have to be mixed with one of the approaches seen above.

Data types

Maps

Sets

Graphs

Trees

Miscellaneous

Sometimes you’re using the right approach but things still don’t work. I that case you can try these small fixes/optimizations.

Fast I/O

Integer overflow

Sources

Improvements?

If you feel like something is missing or you’d like to improve it, PR’s are appreciated.