TopCoder problem "CoolRectangles" used in SRM 362 (Division I Level Three)

Problem Statement

    You're given several rectangles in the cartesian plane with sides parallel to the coordinate axes. A "cool rectangle" is a rectangle in the plane such that each of its edges completely lies on the edges of some input rectangles (these edges may overlap). You want to "compress" the input rectangles by shrinking them into cool rectangles (this will happen instantaneously).

To compress an input rectangle into a cool rectangle, the interior of the cool rectangle must be completely contained within the input rectangle. The interiors of the chosen cool rectangles must not overlap (though their edges may touch), and each cool rectangle may compress at most one input rectangle. Determine the smallest total area of cool rectangles required to shrink all the input rectangles, or return -1 if it is impossible.

The input rectangles will be described by int[]s x1, y1, x2, and y2, where (x1[i],y1[i]) and (x2[i],y2[i]) are opposite corners of the ith rectangle.


Parameters:int[], int[], int[], int[]
Method signature:int compress(int[] x1, int[] y1, int[] x2, int[] y2)
(be sure your method is public)


-x1 will contain between 1 and 30 elements, inclusive.
-x1, y1, x2, and y2 will contain the same number of elements.
-Each element of x1, y1, x2, and y2 will be between -10000 and 10000, inclusive.
-For each rectangle, x1[i] < x2[i], and y1[i] < y2[i].


Returns: 3
We have three non-overlapping squares, so the only cool rectangles are the input rectangles.
Returns: -1
The overlapping squares generate 3 cool rectangles. However, there is no way to choose 2 non-overlapping cool rectangles to use.
Returns: 3
These rectangles intersect in a way that allows for cool rectangles of area 1 and 2 to be used (although there are 11 available cool rectangles).
Returns: -1
Returns: 4
Returns: 2325650

Problem url:

Problem stats url:




PabloGilberto , Olexiy , lovro , ivan_metelsky

Problem categories:

Geometry, Graph Theory, Greedy, Math