Shortcuts in Competitive Programming

, , Leave a comment

Competitive programming is nothing but coding in different languages such as C/C++ and sometimes in Java. While coding in C/C++, macros and typdefs are the only shortcuts that geeks are familiar with. Here are some shortcuts that you can use while coding.

Shortcuts in Competitive Programming

Shortcuts for “common” data types in contests

typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

Infinite

const int INF = 0x3f3f3f3f;

To clear array of integers

memset(arr, 0, sizeof arr);

initialize DP memoization table with -1

memset(memo, -1, sizeof memo);

to simplify: if (a) ans = b; else ans = c;

ans = a ? b : c;

Use a vector to create a dynamic array and to initialize it in one statement. Creates a vector of size N and values as 0.

vector<int> v(N, 0); // OR vi v(N, 0);

to simplify: ans = ans + val; and its variants

ans += val;

index–; if (index < 0) index = n – 1;

index = (index + n – 1) % n;

index++; if (index >= n) index = 0;

index = (index + 1) % n;

 

min/max shortcut to update max/min

ans = min(ans, new_computation);

for rounding to nearest integer

int ans = (int)((double)d + 0.5);

To return true if a value matches a given

number, else return false

if (val == given_no) return true;

else return false;

return (val == given_no)

Well, these were some of the basic shortcuts that can be used while coding. The main motive of coding is to make things easy. One rule that codes follow is to reduce the length of the code. The quality of your code is determined the length of the code. An ideal coder gives out the same result in less lines of code .

So to all the coders out there, enjoy the tips!!!

 

Leave a Reply