What are Ad Hoc Problems in Competitive Programming?
The Ad Hoc problems are problems that cannot be classified anywhere else in the categories with well-studied solutions since each problem description and its corresponding solution are unique. These problems don’t fall under standard categories, there is no specific or general technique exists to solve them. Many Ad Hoc problems are easy, but this does not apply to all Ad Hoc problems.
- Ad Hoc problems frequently appear in programming contests. In ICPC, ≈ 1-2 problems out of every ≈ 10 problems are Ad Hoc problems. In a programming contest, if the Ad Hoc problem is easy, it will usually be the first problem solved by the teams. However, in the cases where solutions to the Ad Hoc problems were too complicated to implement, causing some teams to solve them in the last hour. In an ICPC regional contest with about 60 teams, your team would rank in the lower half (rank 30-60) if you can only solve Ad Hoc problems.
- In IOI 2009 and 2010, there has been 1 easy task per competition day 11, usually an (Easy) Ad Hoc task. If you are an IOI contestant, you will definitely not win any medals for just solving the 2 easy Ad Hoc tasks over the 2 competition days. However, the faster you can clear these 2 easy tasks, the more time that you will have to work on the other 2 × 3 = 6 challenging tasks.
Tips For Ad Hoc Problem
There are some general tips for approaching a problem that appears to be Ad Hoc:
- Writing Down Ideas: Whenever there is an observation that seems useful, write it down as writing down the ideas makes sure that they are not forgotten and can potentially be the solution.
- Don’t get stuck: Don’t get stuck at any specific idea, unless it is appearing to be a complete solution for the problem. It’s a better idea to complete the search as Adhoc problems can waste a lot of time.
- Draw lots of small cases: In order to gain a better understanding of the problem, it is recommended practice to draw a lot of small cases. Draw more cases when-
- There is a problem with debugging.
- If you don’t know how to start with a problem.
- Whenever it is uncertain how to further approach the problem.
- The best idea is to draw more cases and make observations about the properties of the problem.
- Different perspectives: Try to approach the problem from different perspectives. Keep trying the ideas, draw a visual depiction of the problem, try different formulas, different values in the formulas until progress is made.
- Realize pattern in solution: After solving a number of programming problems, try to realize a pattern in solutions. There are certain idioms that are used frequently enough in competitive programming.
- For example from a C/C++ perspective, these idioms might include Libraries to be included like cstdio, cmath, cstring, etc, data type shortcuts, basic I/O routines, loop macros, and a few others.
- C/ C++ competitive programmer can store these in a header file like ‘adhocproblems.h’. With such a header file, the solution to every problem can begin with a simple header file at the beginning of the program #include<adhocproblems.h>.
Categories Of Ad Hoc Problems Set
Let’s discuss some of the categories of the Adhoc problems set:
- Card game: There are lots of Ad Hoc problems related to card games. One of the approaches is to parse the input strings as playing cards have both suits-
- D/Diamond, C/Club, H/Heart, and S/Spades.
- For ranks usual order is: 2 < 3 < . . .< 9 < T/Ten < J/Jack < Q/Queen < K/King < A/Ace12.
- It may be a good idea to map these strings to integer indices.
- For example, one possible mapping is to map D2 → 0, D3 → 1, . . . , DA → 12, C2 → 13, C3 → 14,. . . , SA → 51. Then it becomes much easier to work with integer indices.
- Chess game: Chess is another popular game that appears in programming contest problems and some of these problems are Ad Hoc problems. For example, the combinatorial task of counting how many ways there are to place 8-queens in 8×8 chessboard.
- Other games: Many other games like Tic Tac Toe, Rock-Paper-Scissors, Snakes/Ladders, BINGO, Bowling, etc have also made their way to the programming contests. Knowing the details of these games may prove to be helpful, but most of the game rules are listed in the problem description to avoid disadvantaging contestants who are unfamiliar with the games.
- Palindrome-related problems: A palindrome is a word (or a sequence) that can be read the same way in either direction. The most common strategy to check if a word is palindromic is to loop from the first character to the middle one and check if the characters match in the corresponding position from the back. For example, ‘ABCDCBA’ is a palindrome.
- Anagram-related problems: An anagram is a word (or phrase) whose letters can be rearranged to obtain another word (or phrase). The common strategy to check if two words are anagrams is to sort the letters of the words and compare the results. For example, take wordA = ‘cab’, wordB = ‘bca’. After sorting, wordA= ‘abc’ and wordB = ‘abc’ too, so they are anagrams.