Competitive Programming: Tips before getting started

As I have previously told you, I have started reading a book on CP, and I'll post things from it. Here is the first post.


Tips before starting:

Tip 1: Type Code Faster

Try this typing test at http://www.typingtest.com. Mine was 48 wpm, the book recommends 55-95.If you have much less than ~55, you need to improve it. You can do it at the given link given above.
The book lays great emphasis on it. In case you have poor typing skills, you need to improve it.

Tip 2: Quickly Identify Problem Types

A. I have solved this type before -- I am sure that I can re-solve it again (and fast)
B. I have seen this type before -- I know I cannot solve it yet
C. I have not seen this type

The book says you need to maximize the number of problems you put in type A.

On a technical basis, you need to learn to classify the problems into, for example, Ad Hoc, Complete Search, Divide and Conquer, Greedy, Dynamic Programming, Graph, Mathematics, String Processing, Computational Geometry, so on and so forth.

Tip 3: Do Algorithm Analysis:

The book says: after you have created an algorithm. Analyze it. Modern computers can do up to $10^8$ computations in a second (it was as in 2013, mine in 2019 have a frequency of 3.7 GHz, thus it can do up to $10^9$ operations per second)

Use this number to find if your code will pass the time limit. In case it won't there is no sense in implementing the algorithm. It will be just a waste of time.

Some important points:
  •  k-nested loops of about n iterations each have $O(n^k)$ complexity.
  • A recursive algorithm with b recursive calls per level and goes up to L levels have roughly $O(b^l)$ complexity.
  • int have an upper limit as $2^31-1 ~ 2*10^9$ and long long have an upper limit of $2^63-1~2*10^18$
  • unsigned int have $2^30-1$ and unsigned long long have a limit of $2^64-1$
  • Refrain from coding until you are sure that your algorithm is both correct and fast enough

Tip 4: Master Programming Languages:

The book suggests that being master in c++ is required but being good in java is also required, as Java has powerful built-in libraries and APIs.

Being extremely good in ones favorite language is a desired prerequisite.



Tip 5: Master the art of Testing the code:

The book puts some guidelines for designing good test cases:
  1. Test cases should include sample test cases. As humans are prone to error, create a file named as input and other as output. Copy-paste all the test cases into them and test using them. After compiling output, execute it with I/O redirect './a.out < input > myoutput'. And execute 'diff myoutput output' to highlight any subtle differences, if any exist.
  2. For problems with multiple test cases in a single run, include two identical sample test cases consecutively in the same run. Both must output the same known answers. This helps you determine if you have forgotten to initialize any variables, if you have, first will give correct output next won't.
  3. In test cases, include tricky corner cases.
  4. include large test cases.

Tip 6: Practice and More practice:

No doubt, you must have expected this tip. Practice on uva online judge, sphere online judge, codechef, codeforces, topcoder, etcetera.

No comments:

Post a Comment

Installing albert on ubuntu 19.04

Installing Albert on Ubuntu 19.04... Albert is not still released for ubuntu 19.04. But still, you can install it using the following ...