Competitive Programming: Conquering a given problem
Programming is the process of developing and implementing sets of instructions to enable a computer to do a certain task.The programming contests like ACM ICPC, Google CodeJam, and IEEE Extreme etc. are delightful playgrounds for the exploration of intelligence of programmers.
From knowing the contest at first to being at ACM ICPC Amritapuri Regionals, I have learnt a lot and would like to share some tips for the coders to tackle the contest problems.
During a real time contest, teams consisting of three students and one computer are to solve as many of the given problems as possible within 5 hours. The team with the most problems solved wins, where ‘solved’ means producing the right outputs for a set of test inputs (trivial and secret). Though the individual skills of the team members are important, in order to be a top team, it is necessary to make use of synergy within the team.
However, to make full use of a strategy, it is also important that your individual skills are as honed as possible. You do not have to be a genius as practicing can take you quite far. As far as feel, there are three factors crucial for being a good programming team:
- Knowledge of standard algorithms and the ability to find an appropriate algorithm for every problem in the set
- Ability to code an algorithm into a working program
- Having a cooperative strategy with your teammates
What is an Algorithm?
Algorithm is a step-by-step sequence of instructions for the computer to follow.
To be able to do something competitive in programming contests, you need to know a lot of well-known algorithms and ability to identify which algorithms is suitable for a particular problem (if the problem is straightforward), or which combinations or variants of algorithms (if the problem is a bit more complex).
Ability to quickly identify problem types
In all programming contests, there are only three types of problems:
- I haven’t seen this one before.
- I have seen this type before, but haven’t or can’t solve it.
- I have solved this type before.
In programming contests, you will be dealing with a set of problems, rather than a single problem. The ability to quickly identify problems into the above mentioned context classifications (haven’t seen, have seen, have solved) will be one of key factor to do well in programming contests. As far as my knowledge and repository, a problem is generally classified amongst given categories:
- Mathematics : Prime Number, Big Integer, Permutation, Number Theory, Factorial, Fibonacci, Sequences, Modulus
- Dynamic Programming : Longest Common Subsequence,Longest Increasing Subsequence, Edit Distance, 0/1 Knapsack, Coin Change, Matrix Chain Multiplication, Max Interval Sum
- Graph Traversal :Flood Fill,Floyd Warshal, MST, Max Bipartite Matching, Network Flow, Articulation Point
- Sorting : Bubble Sort, Quick Sort, Merge Sort, Selection Sort, Radix Sort, Bucket Sort
- Searching : Complete Search, Brute Force, Binary Search
- String Processing : String Matching, Pattern Matching
- AdHoc Problems: Trivial Problems
Ability to analyse your algorithm
You have identified your problem. You think you know how to solve it. The question that you must ask now is simple: Given the maximum input bound (usually given in problem description), can my algorithm, with the complexity that I can compute, pass the time limit given in the programming contest.
Usually, there are more than one way to solve a problem. However, some of them may be incorrect and some of them is not fast enough. However, the rule of thumb is: Brainstorm many possible algorithms – then pick the stupidest that works!
It is useful to memorize the following ordering for quickly determine which algorithm perform better asymptotically: constant < log n < n < n log n < n^2 < n^3 < 2^n < n!
Coding the Problem
Follow the principle KISS: Keep it Simple and Smart, keeping all versions working and stepwise refinement of code.
After you have coded with the best algorithm, matching time/space complexity and satisfying the test cases (sample test cases are trivial, so never measure code correctness according to them and try tricky cases too), then submit the solution – ‘ACCEPTED’
Identifying a tricky test case to get the opponent down is as important as solving the problem where your mind works more at the boundary conditions.
To summarise, these are some key points:
- Read all the problems, select the problem in order : easy to tough (if everyone is solving a selected problem, do have a look at it)
- Sketch the algorithms, complexity, conditions, data structures and tricky details
- Brainstorm other possible algorithms (if any) – then pick the stupidest that works
- Do all the Math and select the best algorithm.
- Code it, as fast as possible, and it must be correct.
- Try to break the algorithm – look out for boundary test cases.
The main mantra to succeed a problem is to keep calm, manage your time and have a strategic approach, rest your experience and practice plays the part.
Keep Practicing !!
This article is written by Vinay Garg. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Please Login to comment...