Square Pasture#10556

Square Pasture (Editorial)

Suppose the coordinates of the rectangles are given as follows:

Consider the smallest rectangle that completely covers both original rectangles. Let its opposite corners be (minx, miny) and (maxx, maxy). Then:

The dimensions of the covering rectangle are:

To completely cover both rectangles with a square, the side length of the square must be at least as large as both length and . Therefore, the side length of the minimum square pasture is the maximum of these two values:

side = max(length, width)

Example

Consider two original rectangles:

Let's calculate the boundaries of the resulting rectangle:

The dimensions of the covering rectangle are:

The side length of the covering square is:

side = max(7, 3) = 7

Therefore, the area of the minimum covering square is 7^2 = 49.

Algorithm implementation

Initialize the boundaries of the covering rectangle. Since the coordinates are in the range from to , we can set the initial values as follows:

minx = 10; maxx = 0; miny = 10; maxy = 0;

Read the coordinates of the two original rectangles. For each rectangle, update the boundaries of the covering rectangle.

for (i = 0; i < 2; i++)
{
  scanf("%d %d %d %d", &a1, &b1, &a2, &b2);
  if (a1 < minx) minx = a1;
  if (a2 > maxx) maxx = a2;
  if (b1 < miny) miny = b1;
  if (b2 > maxy) maxy = b2;
}

Calculate the length and width of the covering rectangle.

l = maxx - minx;
w = maxy - miny;

To cover this rectangle with a square, the side length of the square must be at least as large as both its length and its width. Therefore, we choose the larger of the two values.

len = (l > w) ? l : w;

Calculate the area of the square and print the answer.

area = len * len;
printf("%d\n", area);