To solve this problem, we need to find the area of the smallest axis-aligned rectangle that can enclose all given points. The key insight here is that the minimal axis-aligned rectangle is determined by the extreme values of the x and y coordinates of the points.
Approach
- Identify Extreme Coordinates: For all given points, compute the minimum and maximum values of the x-coordinates and the minimum and maximum values of the y-coordinates.
- Calculate Dimensions: The width of the rectangle is the difference between the maximum and minimum x-coordinates. The height is the difference between the maximum and minimum y-coordinates.
- Compute Area: The area of the rectangle is the product of its width and height.
Solution Code
n = int(input())
min_x = float('inf')
max_x = -float('inf')
min_y = float('inf')
max_y = -float('inf')
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)
width = max_x - min_x
height = max_y - min_y
area = width * height
print(area)
Explanation
- Reading Input: We first read the number of points
n. - Initialization: We initialize the extreme values to infinity (for minima) and negative infinity (for maxima) to ensure any valid coordinate will update these values.
- Updating Extremes: For each point, we update the minimum and maximum x and y values.
- Calculating Area: Using the extreme values, we compute the width and height of the rectangle, then multiply them to get the area.
This approach efficiently computes the required area with a time complexity of O(n), where n is the number of points, as we only traverse the list of points once. This is optimal for this problem.
Example: For points (0,0), (2,3), (5,1), the extreme values are min_x=0, max_x=5, min_y=0, max_y=3. The area is (5-0)*(3-0)=15, which is correct.
Edge Cases:
- If there is only one point, the area is zero (since width and height are zero).
- If all points are the same, the area is zero.
This solution handles all cases correctly and efficiently.


作者声明:本文包含人工智能生成内容。