We can number the possible positions of line segment endpoints from (1) to (4(N-1)) in clockwise order around the edge of the square, and then simply map all of the given line segment endpoints to these indices, noting that only their relative order is relevant for determining which proposed cake shapes overlap with one another.
It's possible to show that, for any valid set of proposed cake shapes, there must exist at least one key line segment amongst them such that every polygon in the set overlaps with that line segment. We'll therefore consider all (2N) possible key line segments.
For a given key line segment, each polygon either doesn't overlap with it at all (and so should be ignored), overlaps with all of it (and so may always be used), or overlaps with only part of it. For polygons falling into this final category, we're interested in the maximum combined value of any set of them such that each pair of them overlaps.
The key line segment splits the full set of possible endpoint indices into two sections, which we'll call the X and Y sections. We can map each polygon's endpoints to new indices along these two sections, both numbered starting from the same ("first") endpoint of the key line segment (meaning that one section's sequence of indices is equivalent to increasing original indices, while the other section's is equivalent to decreasing original indices). If a polygon has 3 endpoints in one section and 1 endpoint in the other (as opposed to 2 in each), then we'll shift one of them over to one of the key line segment's endpoints without changing which other polygons it overlaps with.
The result is that, for each line segment of each polygon, one endpoint will have been mapped to a new index in the X section, with the other mapped to a new index in the Y section. This allows us to represent each polygon by a pair of intervals ([x_1, x_2]) and ([y_1, y_2]) (consisting of its two X section indices and its two Y section indices, respectively). With this representation, we can then observe that a pair of polygons don't overlap if and only one of the polygons' X and Y intervals are both strictly to the right (or strictly to the left) of the other polygon's respective X and Y intervals.
If we now visualize each such polygon as a rectangle on a 2D plane, with coordinates based on its X and Y intervals (in particular, with lower-left corner ((x_1, y_1)) and upper-right corner ((x_1, y_1))), we're interested in the maximum combined value of any set of them such that no rectangle is strictly both above and to the right of any other rectangle. This may be accomplished in (O(N log(N))) time with a line sweep over the x-coordinates. We'll let (M_y) be the maximum combined value of a valid set of rectangles encountered so far, such that the lowest of their top-right corners encountered so far has y-coordinate y
, and maintain this with the help of a segment tree:
- When we encounter the left edge of a rectangle with bottom edge
y_1
, we should increase each of (M_{y_1..\infty}) by that rectangle's value (as this rectangle may be added to any such set). - When we encounter the right edge of a rectangle with top edge
y_2
, we should first set (M_{y_2}) to (\max(M_{(y_2+1)..\infty})) (as including this rectangle in any such set would decrease its lowest top-right corner to (y_2)). We should then decrease each of (M_{(y_2+1)..\infty}) by that rectangle's value (as this rectangle may no longer be added to any such set).
Repeating the above process for all (2N) possible key line segments yields a solution with a time complexity of (O(N^2 log(N))) (though a less efficient, (O(N^3)) implementation would also suffice).
For further reading, if we consider mapping the line segment endpoint indices to positions around the circumference of a circle instead (essentially treating the original square as a circle), each proposed cake shape is known as a circle trapezoid. The intersection graph of these polygons (a graph in which there's an edge between two pieces if and only if they overlap) is then known as a circle trapezoid graph. The problem of computing the maximum clique weight in such a graph (equivalent to what's asked for in Cake-Cutting Committee), and other related problems, are explored further in this paper.