Competitive coding tips

on under Competitive coding
5 minute read

Competitive coding is a vast field, frankly even I’m a newbie to the world of competitive coding but I have noticed a few things that I wish I had known before. Just to prevent that regret among my fellow new coders on various online judges, I have compiled some of the facts in this article.

C++-style I/O, is a snail.

Well, this is exclusively for C++ programmers and a disscussion on this may get a bit complicated. I’ve saved it for another article. But here are a few thumb rules (if you want to think of them as ‘rules’) for speeding io in C++ on online judges, only. Add these two lines in the beginning of the code just after the int main(){

ios_base::sync_with_stdio(false);
cin.tie(NULL);

These two lines essentially modify the internal workings of the cout and cin objects . After adding these lines use only cout and cin for I/O (not the C-style printf and scanf) and you might also want to use cout << '\n' in place of cout << endl for a little more speed up.
Or otherwise, if you are a true man you prefer using c-style I/O in C++ just remember including #include <cstdio> and don’t bother adding the two lines I mentioned earlier. In fact printf and scanf are faster than cin and cout. Dont believe me? have a look yourself. Here are three submissions one with C-style I/O in C++ 32456606 and the same one with C++-style (without the two lines) 32456606 and C++ (with those two lines) 33210753. And bang!
you mad, bruh?
The difference is Humongous! Okay?

Maths the spirit

Seriously, no matter how much you run away from it maths is essential for competitive coding. It happens sometimes that you come up with an awesome algorithm but it’s all intution (not to mention O(n^2) and wrong), then the editorial comes out with O(n) solution which involves more mathematics and you end up at nearest ice-cream parlour contemplating the meaning of life ...
Look, It happens to the best of us, no kidding. These problems are generally related to modulo maths or Graph Theory, These are things that they don’t teach in schools and you’ll not hear about them in your college if you are not in pure maths or computer science disciplines, so it’s only natural. The best thing to do, from what my senpais in Competitive Coding told me, is keep on doing problems eventually you’ll find more mathematical types, read up the editorials for problems that you absolutely could’nt solve, understand the concepts along the way and also read up about various algorithms online on portals like geeks-for-geeks.

SIGSEGV

Scared? You should be. The name alone is enough to evoke images of horror in a programmers mind (Images of long staring competition with their screens until the pass out on top of the pile of empty cup noodles they ate last night). Anyway, moving on SIGSEGV stands for SIGnal: SEGmentation Violation, in easy words Segment fault, in easier words tresspassing and for dummies read: bad news. It happens when you try to access (write to) locations of memory which you were not supposed to. for example:

int main(){
	int a[10];
	cin >> a[90];
}

When you enter the input, the program writes to memory address that is 90 integers ahead of a in the memory while the size of a is only 10 integers. Thankfully, there are debuggers like gdb that help you eradicate these pesky imps. You may want to read a bigineer level tutorial here, Unless you’re Chuck Norris, because of course… GDB? what's that

Ignore Complexity, get TLE-ed

At times it’s important to calculate complexity of your algorithm. It may sound silly at start, but hey, it sure comes handy. For example, some typical problems on codeforces have usual constraints 1sec and input size of order 10^6. Now if you come up with an O(n^3) solution for this, you won’t sweat it because it simply won’t work, on the other hand if you get an O(nlog(n)) or O(n) solution it’ll definitely work.

become a typewriter

Typing fast and typing less matters a lot more than what you’d imagine. I prefer making a single template file for all the problems in a contest containing the standard include and int main lines only. You see dependng on the rules some competitiions allow using prewritten code so you can easily create a header file of your own too. this is why the top scores in competitions either belong to people who were able to code extremely fast or people who are smart enough to make a template for themselves.
personally, I sometimes switch to python if the problem contains numbers just larger than 10^18 or it requires something like fast exponentiation and has a weaker time constraint. Python is sometimes the best language for a task. Yes! I said it!
Python is sometimes the best language for a task

Hackermann!

Codeforces has a unique feature called hacks for many contests. The hacking period is generally 1 day from the end of contest. Hacking means entering a test case which causes the program to give wrong result, each succesful hack gets youy 100 points generally. You may want to read up more about it here in the hacks section.

I hope these tips help you guys perform more effectively in contests on various online judges. Ah yes! I forgot the last tip though … here it is! Stay algoed!

Tips, C/C++
comments powered by Disqus