Everything I know about Competitive Programming

Competitive programming, also known as CP, is a mind game. There will be given a problem statement, some inputs, and their outputs with additional constraints like time and space constraints. You have to solve this problem efficiently. If your coder takes more than 1 sec, then it will give a time limit exceeding the error. You can run a maximum of 10^7 operations in 1 sec. Also if your input data is too big and does not fit in the long variable, then you have to use a string to input the data. We can initialize an array with a maximum 10^7 size.

Programming Language

C++ is the most famous language in CP. Nearly everyone uses C++ to do CP as C++ is pretty fast and its STL library(Vector, Map, Set) helps us a lot in solving problems. You can find most of the tutorial vlogs and videos in C++.

Some hacks in C++ for CP:

Header File : #include<bits/stdc++.h>

It includes all the Header files of C++ which saves a lot time of for programmers

User-defined Shortcut

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

This function is used for taking fast input and output.

#define MOD 1000000007

#define MOD1 998244353

#define INF 1e18

#define endl "\n"

#define pb push_back

#define ppb pop_back

#define ff first

#define ss second

#define PI 3.141592653589793238462

#define set_bits __builtin_popcountll #define sz(x) ((int)(x).size())

#define all(x) (x).begin(), (x).end()

#define fo(i,n) for(ll i=0;i<n;i++)

#define po(i,n) for(ll i=1;i<=n;i++)

#define rep(i,a,b) for(ll i = (a); i <= (b); ++i)

typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;

This user-defined shortcut saves a lot of time in contests.

Debugging options

#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;

void print(ll t) {cerr << t;}

void print(int t) {cerr << t;}

void print(string t) {cerr << t;}

void print(char t) {cerr << t;}

void print(lld t) {cerr << t;}

void print(double t) {cerr << t;}

void _print(ull t) {cerr << t;}

template <class T, class V> void print(pair <T, V> p);

template void print(vector v);

template void print(set v);

template <class T, class V> void print(map <T, V> v);

template void print(multiset v);

template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.ff); cerr << ","; print(p.ss); cerr << "}";}

template void *print(vector v) {cerr << "[ "; for (T i : v) {*print(i); cerr << " ";} cerr << "]";}

template void *print(set v) {cerr << "[ "; for (T i : v) {*print(i); cerr << " ";} cerr << "]";}

template void *print(multiset v) {cerr << "[ "; for (T i : v) {*print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void *print(map <T, V> v) {cerr << "[ "; for (auto i : v) {*print(i); cerr << " ";} cerr << "]";}

We can do faster debugging by incorporating these codes. You want to print an array v, then write debug(v). The same goes for variables, sets, and maps. If a variable name is tt, then write debug(tt) to print it. Thus, we can debug your code pretty fast.

You can also use other languages like Java, C, Python, etc to do CP.

Best Resources for Practicing CP

Codeforces

Codeforces is the best website where you can practice problems. There are over 7000 problems there. As a beginner, you can start solving problems rating of 800. Try this problem :

Watermelon: https://codeforces.com/problemset/problem/4/A

This is one of the basic problems where you can start your CP journey. Gradually you should try to solve difficult problems. When you select problems, make sure that problem ratings are less or equal to your current rating + 200. If you try to solve harder problems at first, you will become frustrated. So, solve problems gradually. Remember, it is a marathon.

Codeforces Rating Divison

Image Source: EbTech's blog

Insights :

  • You are in the top 1% once you become a Master

  • You are in the top 20% once you become an Expert

  • 94% of the coders cannot participate in Div.1 contests

  • 60% of the coders are eligible to participate and rated in Div.4 contests

There are 4 types of Contests: DIV.1, DIV.2, DIV.3, and DIV.4.

DIV.3 and DIV.4 contests are for beginners. You can start giving contests by participating in these contests. Remember, if you want to do well in CP, you have to participate in regular contests. There is no alternative to it.

You can solve problems by using tags. Suppose, you want to solve problems about Number Theory. So, you can use Tags to find the problems regarding Number Theory.

CodeChef

This is another popular website for practicing CP. CodeChef rating systems are quite different than Codeforces.

A Star Studded Rating System | CodeChef

CodeChef beginner's problems are quite easy compared to Codeforces. To improve your rating such as from 1* to 2*, CodeChef will guide you by giving some practice problems. CodeChef has also a paid version. In the paid version, you will probably get video solutions and customized test cases. However, it is not needed to buy the paid version. There are already a lot of free tools on the Internet where you can learn these kinds of stuff.

You can participate online contests in every Wednesday at 8.00 pm(Bangladesh). There are two types of contests: Long and Short Contest

AtCoder

AtCoder DP problems are quite good. If you want to solve Dynamic programming-related problems, you can practice these problems. Also, you can participate in contests at AtCoder.

CSES

CSES 300 problems are a life savior. Almost every concept that is used in programming can be learned by solving these problems. If you want to do well in CP, I highly recommend you solve as much as problems from CSES.

VJudge

You can select problems from different websites like Codeforces, CodeChef, Hackerrank, etc, and can arrange contests with your friends. Usually this website is used in onsite CP contests.

CP Algorithms

You can learn different algorithms like BFS, DFS, Combinatorics, etc from the CP Algorithms Website. The good point is that in every article, they provide related problems regarding the algorithm which you can practice to enhance your skills about that particular topic.

Geeks for Geeks

You can learn any algorithm and data structure by reading GFG articles. Also, you can practice problems regarding the algorithm.

Youtube Channels

Errichto Algorithms : You can learn Binary Search, Prefix sum, contribution techniques, etc from this Channel.

Luv: You will find a dedicated playlist for CP in this channel.

Colin Galen : Galen's Guidelines for becoming better at CP are effective.

William Lin : You must watch this video - Starting Competitive Programming - Steps and Mistakes from his channel.

TLE Eliminators - by Priyansh : Recent Codeforces problem solutions are discussed in this channel.

CodeNCode : You can number theory from this channel. A detailed playlist about number theory containing 38 videos is featured here.

Tushar Roy - Coding Made Simple : Good channel for learning Data Structures and Algorithms.

Aditya Verma : Best channel for learning Dynamic Programming

Code Editor

You can use Vs code(Visual Studio Code) or Sublime Text. In Vs code, install CPH Judge. This will help you to test code efficiently. In Mac, make sure to change the default Clang path to the g++ compiler path. In Clang, some properties of C++11 don't work. That's why, it is important to change the default compiler path. You can watch this video if you are a Mac User.

Video link : https://youtu.be/ZjvIC_hTN80?si=e2l1qeW_Vvc-XdeC

Must Read Topics needed for CP

STL

Vector, String, Map, Pair, Set, Multiset, Queue, Priority Queue, Deque, Programming Contest Template With Debugger

Basic Algorithms And Data Structures

Recursion, Basic Dynamic Programming, Merge Sort, Partial Sum, Contribution Technique, Binary Search (Basic Binary Search, Upper Bound, Lower Bound, Integer Bisection, Fractional Bisection, Binary Search With Prefix Sum), Policy-Based Data Structure or PBDS (Basic PBDS, PBDS With Partial Sum And Contribution Technique), Sliding Windows, Two-Pointer, Structure, Structure Sorting

Basic number theory

Harmony Series, Prime Generation or Sieve of Eratosthenes, Prime Factorization, Number of Divisors, Sum of Divisors, Euler Phi Function, Modular Arithmetic, Fermat's Little Theorem

Basic graph theory and dynamic programming

Graph, Tree, Adjacency Matrix, Adjacency List, Depth-First Search (DFS), Breadth-First Search (BFS), The Shortest Path On The Unweighted Graph, Bi-Coloring of Bipartite Graph, Topological Sort, Cycle Finding, 0-1 Knapsack, Coin Change, DP Solution Print, Shortest Path in Graph

Intermediate graph theory, Dynamic programming, and Segment tree

Dijkstra, Belman Ford, Floyd Warshal, Disjoint Set Union, Minimum Spanning Tree, Longest Common Subsequence, Longest Increasing Subsequence in Both DP and Binary Search, Bitmask DP, Digit DP, Segment Tree, Lazy Propagation, Merge Sort Tree

Advanced graph theory

Disjoint Set Union, Minimum Spanning Tree , Sparse Table, and Lowest Common Ancestor

Source: CPS Academy

Thanks for reading. You can subscribe to my newsletter to get more such content.

My Youtube Channel : Abrar Eyasir

Linkedin Link : https://www.linkedin.com/in/abrar-eyasir-4423b7216/

Medium Link: https://medium.com/@eyasir2047