I have been solving problems and challenges on CodingGame in C++ for a while now and I thought I would like to give back something to the CG community.It is slightly a long post but I guarantee that it will help improve, so do take some time to read this.
So here are a small list tricks that you can use to optimize your coding efficiency. I have tried to sort them in increasing order of difficulty
#Macros :
Loop Macros
Macros are some of the most simplest way to simplify your code and also to type faster. I usually use them when there are long lines of code which keep repeating.So here are a few simple macros that you can use :
#define FOR(i,a,b) for(int i=a;i<=b;++i)
This macro is probably super useful because it makes creating a for loop look like child’s play.This simply iterates starting from value a to b (both inclusive)
Another similar macro is :
#define REP(i,n) FOR(i,0,(int)n-1)
This should be declared after the first one.Although this looks similar to the first one I its really helpful to have this.It simply loops from 0 to n-1 This makes reading arrays very easy.
int a[10000];
REP(i,n)
scanf("%d", &a[i]);
See its that simple. These 2 MACROS are very useful and I would highly recommend that you use them to write simpler and faster code.
If you want to loop in reverse then you can use this macro :
#define FORD(i,a,b) for(int i=a;i>=b;--i)
Debuging Macro
Everyone knows debuging the error is a pain in the ass already and to sit and type fprintf(stderr, ...);
everytime is a real bum.So you could use a neat macro like :
#define DB(s,x) fprintf(stderr,s,x)`
Most of you already know this and probably use a similar macro in your code. But the problem comes when you have more than 1 argument for your debug message. For example if you want to print out the x and y coordinates variable you will have to do something like.
DB("X = %d\n", x);
DB("Y = %d\n", y);
Most of them don’t mind doing this. But what if there is a magical way to pass more than 1 argument to your debug macro ? That would save a lot of time and make your code neat and clean. And the answer is YES, there is a way to do that and it is :
#define DB(format,...) fprintf(stderr,format, ##__VA_ARGS__);
The __VA_ARGS__
stands for variable arguments and you can pass any number of arguments just like you do to a printf()
function. So you could now use that macro like :
DB("Inside function");
DB("x = %d", x);
DB("a = %d | b = %d | c = %d | d = %d |", a, b, c, d);
List of a few super useful macros
-
#define SORT(a,n) sort(begin(a),begin(a)+n)
Simple way to sort arrays/vector,etc -
#define REV(a,n) reverse(begin(a),begin(a)+n)
Simple way to reverse an arrays/vector,etc -
#define MS0(x) memset(x,0,sizeof(x))
To initialize the value of an entire array to 0 -
#define ll long long
To avoid typing long long every god damn time -
#define pb(x) push_back(x)
To add an element to a vector (See STL section) -
#define mp(a,b) make_pair(a,b)
To make a pair (See STL section)
Power of S.T.L
I know to a beginner this sound like something straight out a Sci-Fi movie.Most of you might have heard this word somewhere or the other. It stands for Standard Template Library The
At first I was intimidated by STL. I was like what is this cool thing that everyone is talking about. Basically it creates a container for a specific data structure and has all the functions that the data structure needs, like push(), pop(), insert(), erase(), etc
They are super useful and is must if you want to solve Hard and Very-Hard problems. You have no idea how many times i used the queue stl to implement a quick BFS I did some googling and found some nice tutorials on YouTube :
- Vector : https://www.youtube.com/watch?v=5gMJUdNXqX8
- Map : https://www.youtube.com/watch?v=yW9OQu2XNNA
- Pair : https://www.youtube.com/watch?v=xWTYXFfdirY
- Queue : https://www.youtube.com/watch?v=xYLk66M8OIc
- Stack : https://www.youtube.com/watch?v=t0hJ7YOWwBY
There are many more STL containers available but I think these are sufficient for most problems.
# :white_check_mark: General Tips - Try to understand how to use **reference websites**. I personally use http://www.cplusplus.com/reference/ to look up something that I am not sure or I just **google it**. - Always use built-in functions rather than writing your own implementation. Not only does this save time but its also much faster and is **highly,highly optimized**.
My Template
Usually every time I start a new problem I have like a template that I use so that I can get started faster and not have to worry about typing #include< blah blah blah>
every time I begin.Here is my template, feel free to modify it as you see fit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <stack>
#include <vector>
using namespace std;
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define REP(i,n) FOR(i,0,(int)n-1)
#define #define DB(format,...) fprintf(stderr,format, ##__VA_ARGS__);
#define MS0(x) memset(x,0,sizeof(x))
#define SORT(a,n) sort(begin(a),begin(a)+n)
#define REV(a,n) reverse(begin(a),begin(a)+n)
#define ll long long
#define sp system("pause")
struct point{
int x, y;
};
int a[100000];
int main(){
int n;
scanf("%d", &n);
while(1){
//Game Loop
}
return 0;
}