Editorial for Back From Summer '19 P4: Wesley And Cake


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: wleung_bvg

For all subtasks, we can observe that the largest axis-aligned square will always share an edge with the border of the cake. This is because as you move away from the centre the distance between two slopes will always be increasing, thus allowing a square that did not touch the border to be moved towards the border without crossing any cuts. It can also be shown that the cuts are symmetrical about the line y = -x.

Subtask 1

For the first subtask, we can see that when M = 1, there is always a square of side length N that does not intersect the cut.

When M = 2, we can see that there are two cases. The first is when both slopes have the same sign. In this case, once again there is always a square of side length N that does not intersect the cut. The second case is when the two slopes have opposite signs. In this case, we can see that the two lines create four different regions (though only two are distinct due to symmetry). In order to find the largest square in each of the regions, we need to find a line parallel to the border such that the distance from the line to the border is equal to the distance between the two given slopes at that line. This can be done by solving the linear equation (s_1 - s_2) (N - L) = L where L is the side length of the square and s_1, s_2 are the given slopes. This equation will handle the case where the square shares an edge with the vertical border. To handle the horizontal border, we can take the negative reciprocal of the slopes and use a similar equation.

Time Complexity: \mathcal O(M)

Subtask 2

For the second subtask, we can see that the cake is divided into many triangular regions (with a few exceptions at the regions containing the corners of the cake). The slopes that bound these regions can be determined after sorting the slopes. For the regions created by two slopes with different signs, we can use a method similar to subtask 1. For slopes with the same sign, it can be shown that one of the corners of the square will be at the intersection of one of the slopes, and the border of the cake. To determine the other corner, we can find the intersection of the square's diagonal and the other slope. When the region is triangular, it can be seen that there is only one location where a valid square can be placed. In the case of the region containing the corner of the cake, the location furthest away from the cake's corner should be used as the corner for the squares.

Time Complexity: \mathcal O(M \log M)


Comments

There are no comments at the moment.