Skip to main content

CCC '20 J3

DMOJ Link: https://dmoj.ca/problem/ccc20j3

Problem Interpretation

We are given the coordinates of various points, and want to find the smallest rectangle containing all of them.

caution

Keep in mind that points on edges of the rectangle are not considered to be contained within it, per the following line of the problem statement:

Points on the frame are not considered inside the frame.

Solution Sketch

First find the minimum and maximum xx and yy coordinates. Call them xminx_{\text{min}}, xmaxx_{\text{max}}, yminy_{\text{min}} and ymaxy_{\text{max}} respectively.

In order to contain all the points given, our rectangle needs to satisfy two properties:

  • Include all x-coordinates from xminx_{\text{min}} to xminx_{\text{min}}.
  • Include all y-coordinates from yminy_{\text{min}} to ymaxy_{\text{max}}.

Evidently, the smallest valid rectangle meeting these criteria has bottom-left corner (xmin1,xmax1)(x_{\text{min}}-1, x_{\text{max}}-1) and upper-right corner (ymin+1,ymax+1)(y_{\text{min}}+1, y_{\text{max}}+1).

Implementation

tip

If you are struggling with input/output, the article on I/O may be helpful.

We first read in nn, the number of points.

Then, we initialize variables representing xminx_{\text{min}}, xmaxx_{\text{max}}, yminy_{\text{min}} and ymaxy_{\text{max}}. As the problem constraints guarantee that all coordinates are between 00 and 100100, we start minimum values at 100100 to ensure they will be overwritten once we process the first point. For similar reasons, we start maximum values at 00. (Initializing the variables in this manner avoids needing to handle the first point separately.)

Next, we read in the nn points and update xminx_{\text{min}}, xmaxx_{\text{max}}, yminy_{\text{min}} and ymaxy_{\text{max}} as needed.

At the end, we output xmin1,xmax1x_{\text{min}}-1, x_{\text{max}}-1 and ymin+1,ymax+1y_{\text{min}}+1, y_{\text{max}}+1.

n = int(input())
min_x, max_x = 100, 0
min_y, max_y = 100, 0
for _ in range(n):
x, y = map(int, input().split(","))
min_x = min(min_x, x)
max_x = max(max_x, x)

min_y = min(min_y, y)
max_y = max(max_y, y)
print(f"{min_x - 1},{min_y - 1}")
print(f"{max_x + 1},{max_y + 1}")