内容中心

2026年比较好的型钢通过式抛丸机厂家推荐与选型指南-江苏巨龙铸造机械有限公司

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

  1. 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.
  2. 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.
  3. 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

  1. Reading Input: We first read the number of points n.
  2. Initialization: We initialize the extreme values to infinity (for minima) and negative infinity (for maxima) to ensure any valid coordinate will update these values.
  3. Updating Extremes: For each point, we update the minimum and maximum x and y values.
  4. 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.

江苏巨龙铸造机械有限公司

江苏巨龙铸造机械有限公司



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

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?