This commentary is meant to give a brief outline of what is required to solve each problem and what challenges may be involved. It can be used to guide anyone interested in trying to solve these problems but is not intended to describe all the details of a correct solution. We encourage everyone to try and implement these details on their own using their programming language of choice.
Attempts to solve past problems can be submitted on the CCC Grader. Also, after a contest is complete, official test data is available by clicking the corresponding "Download" button on our Past Contests webpage.
Determining whether or not Besa can buy her desired number of tickets
involves using an if statement. There are different ways to
structure this. One way is to use the condition \(B+P>T\) and print N when
the condition is true and print Y followed by the value of
\(T-P-B\) when the condition is
false.
There are always exactly six input values for the second problem. It can be solved by storing the last value, the difficulty factor, in a variable and storing the five scores in five different variables or in a sequential data structure (e.g. a list, an array, or a vector). However, it is interesting to note that storing these values is not absolutely necessary. As we read in each score, we can (i) add it to a running total of the scores, (ii) maintain a "current lowest score" \(L\) which is initialized to the value of 10, and (iii) maintain a "current highest score" \(H\) which is initialized to 0. Then the athlete's overall score to be printed is \((S-H-L)\times D\) where \(S\) is the sum of the five scores and \(D\) is the difficulty factor. For the sample input, this would be \((35-0-10)\times3 = 75\). Notice how this equals \((7+8+10)\times3\) as given in the explanation of the output for the sample input.
A full solution to the third problem on the contest requires
repeatedly solving its first subtask where Ngoc and Minh each have only
one candy. An if statement can handle the small fixed
number of possibilities in this case. To handle additional candies, the
if statement can be placed inside a while loop
with exactly one or two candies eaten per iteration of the loop.
The body of the loop can be structured to simulate eating one or two candies. That is, each sequence of candies (letters) can be represented by a string. When a candy is eaten, the first character of the string can be removed. Depending on which programming language or string type we use, this might mean replacing the string by a new string that is a substring or slice of the larger string. This approach will solve the second and third subtasks, but a less complex loop condition is needed for the second subtask.
Now, like your dentist, we recommend that you take a very long time if you decide to consume 50 candies. However, a computer program can simulate this in microseconds. That is, efficiency is not a concern until the fourth subtask where at least one of Ngoc and Minh will eat an absurd amount of candy. To ensure we do not exceed the time limit in this case, we must ensure each iteration of the loop is quick. It is possible to do this with the simulation approach discussed above, and a carefully chosen data structure and an appropriate removal function/method. However, a cleaner solution is to leave the original string unchanged. We can accomplish this by maintaining the index of the next candy at the front of each line. That is, we will consider the candies to the left of each index to be removed even though they remain part of the string. This way, each iteration of the loop involves using a solution to the first subtask to determine whether to increment one or both of the indices by one.
In order to count the number of times the snail enters a slimy square, we can keep track of which squares are slimy as the snail crawls from square to square. The different subtasks correspond to different ways we might represent a set of slimy squares.
For the first two subtasks, we can represent every square that the snail might enter. This two-dimensional grid of squares can be represented as a sequence of sequences (e.g. list of lists, array of arrays, or a vector of vectors). Alternatively, the two-dimensional grid can be represented as one long sequence: the first row, followed by the second row, followed by the third row, etcetera. For the first subtask, the snail can crawl up to \(20 \times 20 = 400\) squares south and also up to 400 squares east. Therefore, the snail starts at the corner of a \(401 \times 401\) grid. The second subtask is more challenging because it requires us to realize that it involves the snail starting in the middle of a \(801 \times 801\) grid.
For the third subtask, the snail enters up to \(20 \times 1200\) squares in each direction so it is difficult to fit the grid within the memory limits of the CCC grader. Instead, we can store only the set of slimy squares with the understanding that if a square is not slimy, then it is not in our set. Then for every snail movement, we can check to see if the square the snail is entering is in our set. The set can be represented using a sequential data structure and even if we search through the entire structure for every movement the snail makes, we should finish within the time limit.
In the final subtask, the snail can move extremely far from its
initial position so running time is a bigger concern. To solve this
subtask, we need to be able to very quickly determine whether a given
square is in our set of slimy squares. A data structure that supports
"fast look-up" is required (e.g. a dictionary in Python, a
HashSet in Java, or an unordered_set in
C++.)
An alternative solution is to build a sequence of every square that the snail enters including duplicates. The correct answer will be the length of this sequence minus the number of distinct squares in the sequence. This is because if a square the snail enters appears \(x\) times, it will be slimy exactly \(x-1\) of those times. If we can count the number of duplicates in the sequence efficiently (e.g. perhaps by sorting the list), this can lead to a solution that meets the time limit and passes the final subtask.
The last challenge of the 2026 CCC Junior Competition requires answering \(Q\) questions about \(N\) parking spots illuminated by \(L\) lights.
For the first subtask, \(L \leq 1\).
If there isn't a light, the answer to every question is N.
Otherwise, we can determine if parking spot \(i\) is illuminated by checking if \(P-S \leq i \leq P+S\) where \(P\) is the parking spot directly below the
light and \(S\) is the
spread of the light (how many adjacent parking
spots it illuminates on either side).
For the other three subtasks, upper bounds on the values of \(Q\), \(N\) and \(L\) can tell us which algorithms will allow us to answer the questions within the time limit.
For the second subtask, \(L\) and \(Q\) are small so for every question, we can consider each light to determine whether or not a parking space is illuminated.
For the last two subtasks, \(L\) and \(Q\) are both very large so we can no longer consider each light to answer each question. Instead, we need to do some work before starting to answer the questions. Specifically, we can construct a sequence \(A\) (for "answers") where position \(i\) in the sequence records whether or not parking spot \(i\) is illuminated. As for the previous problems, this sequence could be a list, array, or vector with care taken to account for the fact that a sequential structure probably starts indexing at \(0\).
When \(N\) is small, we can build \(A\) by looping through each light and updating \(A\) for each parking spot it illuminates. Alternatively, we can loop through each parking spot and for each light, check if it illuminates the parking spot. Either approach will solve the third subtask.
For the final subtask, \(N\) is very large so we need to be more clever. Below is the outline of two possible approaches that consider the intervals illuminated by each light. That is, for a light above spot \(P\) and with spread \(S\), the associated interval is \([P-S,P+S]\).
"Merge" the intervals. For example, the intervals in the sample input are \([1,2]\), \([2,6]\) and \([8,8]\) and they can be merged into the intervals \([1,6]\) and \([8,8]\). There are several ways to do this. It is tricky, but the key is to ensure we don't loop through the lights or parking spots too often (i.e. only a small fixed number of times like once or twice).
Start with an initial value of \(v(i)=0\) associated with each parking spot \(i\). Then for each interval \([a,b]\) add one to \(v(a)\) and subtract one from \(v(b+1)\). When done, \(v(1)+v(2)+\cdots+v(i)\) will be the total number of lights that illuminate parking spot \(i\). It is possible to compute these sums in one loop and then use them to quickly answer each question.
Above, each value \(P-S\), \(P+S\), \(i\), \(a\) and \(b\) represents a parking spot and we are ignoring the requirement that this means it must lie inside the interval \([1,N]\). This cannot be ignored when implementing the ideas.