Editorial for Mock CCC '26 J3 - Holy Moly's Wall
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Constraints: and
With very few number of holes and small coordinate bounds, this subtask is intended to award pretty much any correct solution, even if it is quite inefficient.
You can completely brute force this subtask any way you would like.
Subtask 2
Constraints: and
Since square frames are centered at and axis-aligned (one pair of sides are parallel with the x-axis, and another pair of sides is parallel with the y-axis), let half of the side-length of a square frame be
. Then the frame's perimeter is defined by it's:
Top Side:
Bottom Side:
Left Side:
Right Side:
If any holes lie at coordinates that satisfy with one of these sides, it lies on the frame's perimeter of when the frame has a side length of .
We can begin our implementation by storing the position of holes in a 2D boolean array that is . All coordinates are added by
to account for negative positions (most languages don't allow for negative indices).
Then, we can simply try all possible values of
within the range
to
to see if a frame of size
has enough holes.
To do this we loop over all sides and increment a count when encountering a hole.
If the number of holes on all sides (the perimeter) is
, we know it is valid.
Example of implementation is shown below:
for (int s = 1; s <= 1000; s++) { // For every frame half side length
int count = 0; // Number of holes lying on the specific frame size's perimeter
for (int x = -s; x <= s; x++) { // Check top side for holes
if (holes[x+1000][s+1000]) {
count++;
}
}
for (int x = -s; x <= s; x++) { // Check bottom side for holes
if (holes[x+1000][-s+1000]) {
count++;
}
}
for (int y = -s+1; y <= s-1; y++) { // Check left side for holes
if (holes[-s+1000][y+1000]) {
count++;
}
}
for (int y = -s+1; y <= s-1; y++) { // Check right side for holes
if (holes[s+1000][y+1000]) {
count++;
}
}
if (count >= k) {ans++;} // If there are enough holes on the perimeter, this is a valid frame size that can be used
}
Be careful to not double count holes in the corners.
Let be the range that coordinates can span.
Time complexity:
Space Complexity:
Subtask 3 and Full Solution
Constraints: and
A solution using subtask 2's technique will not pass subtask 3. A 2D array can not hold the massive range and will MLE or segfault. Even if you use a map, checking every coordinate on each frame size's perimeter explicitly is too slow. Therefore, we need a more mathematical observation.
We first observe that a point lies on the perimeter of side length
if and only if
. The full mathematical proof using the perimeter definitions stated in subtask 2 solution is left as an exercise to the reader, but the observation is quite intuitive.
So instead of iterating over all frame sizes (all ) and checking points along the perimeter, we can reverse this logic. For every hole
, compute
, and maintain a frequency array
count[S] that counts how many holes lie on the perimeter of the square with half side length . After processing all
holes, iterate over each
and merely check if
count[S] >= K.
We don't need to explicitly construct the grid or scan any perimeters.
Let be the range that coordinates can span. Since finding what frame size a hole belongs to is as simple as
, each point contributes to
time. Only at the end do we go over all
within the count array to check if its valid. Therefore:
Time complexity:
Space Complexity:
Comments