8 Amazing C++ Tricks to use in a programming contest
Programming contests can manage their time for each programmer. It is very rare that a programmer starts programming when he sees the puzzle or the problem. Because the main specialty of becoming a programmer is to learn the design process. What this means when you solve the puzzle in your head, you are ready to solve these puzzles with your code.
So here is the problem, contests can be divided into two main axes:
1) Design process
2) Encoding process
Since the coding of an already designed problem can be very simple, everything becomes a time management.
In this publication, you will find tips to help you make the most of your time at competitions. In addition, technical interviews can be linked with these tricks, so the interviewer can see what you really know. It will be fun, so let’s start
Table of Contents
1) Using auto to declare dataypes can save a lot of time during the programming competitions.
If a variable is set to auto, the compiler determines its type at the time of compilation. For example,
2) This is extremely useful with regard to iterators.
To card on card> card instead of writing this,
1
|
for(map<int,vector>:: iterator it = Map.begin(); it != Map.end(); it++ ){}
|
it is possible to write this.
1
|
for(auto it = Map.begin(); it != Map.end(); it++ ){}
|
There is an even shorter way to write it.
1
|
for(auto &it : Map){}
|
This type of loops is called loops based on range, previously compatible with Java. Note that this is not an iterator in loops based on range. So, to access the items, you should be used instead.
3) Loop in C-string
1
2
|
char s[100];
for (int i = 0; s[i]; ++i) { ... }
|
4) Testing if not negative 1
1
|
if (~x) { ... }
|
This works because the negative numbers use the complement two, so -1 is represented as all in the binary. The tilde (~) operation reverses all bits of the variable so that -1 becomes zero and any other number becomes nonzero. The if condition is valid if it is not null, so x holds for any value other than zero.
5) Order array without losing original order
1
2
3
4
5
|
int a[100], p[100];
// receive input
for (int i = 0; i < n; ++i) scanf(“%d”, &a[i]), p[i] = i;
sort(p, p+n, [=](int i, int j) { return a[i] < a[j]; });
|
This is extremely useful!
6) Making Input Output faster
1
|
std::ios_base::sync_with_stdio(false);
|
just after the main()
and see the magic.
I/O become nearly as fast as using scanf() and printf().
7) Simplification with predefined
Most algorithms in the STL are very useful (eg sorting, searching, copying, account, transforming, …), but to be as general as possible, they instead take a number of iterators a container, leaving most of the time it ends writing things like:
1
|
std::sort(std::begin(my_container), std::end(my_container));
|
as you usually want to sort the whole container. Having a single macro like:
1
|
#DEFINE WHOLE(X) std::begin(X),std::end(X)
|
simplifies the command to:
1
|
std::sort(WHOLE(my_container));
|
which is also easier to read.