Editorial for COCI '11 Contest 5 #5 Blokovi
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us observe a stable arrangement of rectangles. The stability condition requires that the X-barycentre of all rectangles other than the lowest one has distance of at most unit from the X-centre of the lowest rectangle. If we also factor in the mass of the lowest rectangle, we obtain the X-barycentre of the whole arrangement which also has distance of at most unit from the X-centre of the lowest rectangle.
Let be the difference of the x-coordinate of the rightmost point of some rectangle in the arrangement and the X-barycentre of the arrangement. This value stays constant if the arrangement is translated along the X-axis.
Let us denote a stable arrangement of the top rectangles with . We will deem as optimal if there exists no arrangement of the top rectangles with a larger value than .
The required solution of the problem is , for some optimal .
The optimal arrangements can be determined using dynamic programming. For each state, we memorize and the total mass of the current optimal arrangement.
The trivial case is , with values , mass of the last rectangle in input.
Determining from :
The rectangle (counting from the top) is placed under the current arrangement, with two possible cases:
- the X-barycentre of coincides with the upper right corner of the new rectangle
- the X-barycentre of coincides with the upper left corner of the new rectangle
We simply select the new arrangement with the larger value among the two possibilities.
Comments