Axis-Parallel Rectangle

本文介绍了一个关于在二维平面上寻找包含至少K个点的最小面积矩形的问题,并提供了一种通过枚举来解决该问题的方法。输入为N个点坐标,输出是最小矩形的面积。

D - Axis-Parallel Rectangle


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

We have N points in a two-dimensional plane.
The coordinates of the i-th point (1≤iN) are (xi,yi).
Let us consider a rectangle whose sides are parallel to the coordinate axes that contains K or more of the N points in its interior.
Here, points on the sides of the rectangle are considered to be in the interior.
Find the minimum possible area of such a rectangle.

Constraints

  • 2≤KN≤50
  • −109≤xi,yi≤109(1≤iN)
  • xixj(1≤i<jN)
  • yiyj(1≤i<jN)
  • All input values are integers. (Added at 21:50 JST)

Input

Input is given from Standard Input in the following format:
N K  
x1 y1
:  
xN yN

Output

Print the minimum possible area of a rectangle that satisfies the condition.

Sample Input 1

Copy
4 4
1 4
3 3
6 2
8 1

Sample Output 1

Copy
21
One rectangle that satisfies the condition with the minimum possible area has the following vertices: (1,1)(8,1)(1,4) and (8,4).
Its area is (8−1)×(4−1)=21.

Sample Input 2

Copy
4 2
0 0
1 1
2 2
3 3

Sample Output 2

Copy
1

Sample Input 3

Copy
4 3
-1000000000 -1000000000
1000000000 1000000000
-999999999 999999999
999999999 -999999999
Sample Output 3
3999999996000000001
Watch out for integer overflows.
 
 
 
// N 个点,选出一个最小的矩形,包括至少 k 个点,求这最小的矩形的面积
// 枚举,疯狂枚举就行
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define MOD 998244353
 4 #define INF 0x3f3f3f3f3f3f3f3f
 5 #define LL long long
 6 #define MX 55
 7 struct Node
 8 {
 9     LL x, y;
10     bool operator < (const Node &b)const{
11         return x<b.x;
12     }
13 }pt[MX];
14 
15 int k, n;
16 
17 int main()
18 {
19     scanf("%d%d",&n,&k);
20     for (int i=1;i<=n;i++)
21         scanf("%lld%lld",&pt[i].x, &pt[i].y);
22     sort(pt+1,pt+1+n);
23     LL area = INF;
24     for (int i=1;i<=n;i++)
25     {
26         for (int j=i+1;j<=n;j++)
27         {
28             LL miny = min(pt[i].y, pt[j].y);
29             LL maxy = max(pt[i].y, pt[j].y);
30             for (int q=1;q<=n;q++)
31             {
32                 if (pt[q].y>maxy||pt[q].y<miny) continue;
33                 int tot = 0;
34                 for (int z=q;z<=n;z++)
35                 {
36                     if (pt[z].y>maxy||pt[z].y<miny) continue;
37                     tot++;
38                     if (tot>=k)
39                         area = min(area, (maxy-miny)*(pt[z].x-pt[q].x));
40                 }
41             }
42         }
43     }
44     printf("%lld\n",area);
45     return 0;
46 }
View Code

 

 
 

转载于:https://www.cnblogs.com/haoabcd2010/p/7673891.html

import numpy as np import matplotlib.pyplot as plt # 获取用户输入 bike_width = float(input("请输入自行车宽度(米): ")) bike_length = float(input("请输入自行车长度(米): ")) parking_length = float(input("请输入停车场长度(米): ")) parking_width = float(input("请输入停车场宽度(米): ")) aisle_width = float(input("请输入通道宽度(米): ")) # 定义权重 convenience_weight = 0.6 capacity_weight = 0.4 # 生成不同角度 angles = np.linspace(0, np.pi/2, 100) # 初始化最佳角度和最佳得分 best_angle = 0 best_score = 0 # 存储每个角度的得分 scores = [] # 假设斜放区域占比为 0.5 slant_ratio = 0.5 # 模拟实时占用率(实际应用中应从传感器获取) occupancy_rate = float(input("请输入停车场当前占用率(0 - 1 之间): ")) # 定义不同占用率下的间隙宽度 def get_gap_width(occupancy_rate): if occupancy_rate < 0.2: return 0.3 # 低占用率,大间隙 elif occupancy_rate < 0.8: return 0.2 # 中等占用率,适中间隙 else: return 0.1 # 高占用率,小间隙 bike_gap = get_gap_width(occupancy_rate) for angle in angles: # 避免除以零 if np.isclose(angle, 0): sin_angle = 1e-10 # 避免除以零 cos_angle = 1 elif np.isclose(angle, np.pi/2): sin_angle = 1 cos_angle = 1e-10 # 避免除以零 else: sin_angle = np.sin(angle) cos_angle = np.cos(angle) # 计算斜放区域可停辆数 slant_width = parking_width * slant_ratio - aisle_width slant_rows = int(slant_width / ((bike_width / sin_angle) + bike_gap)) slant_columns = int(parking_length / ((bike_length / cos_angle) + bike_gap)) slant_capacity = slant_rows * slant_columns # 计算平行放区域可停辆数 parallel_width = parking_width * (1 - slant_ratio) - aisle_width parallel_rows = int(parallel_width / (bike_width + bike_gap)) parallel_columns = int(parking_length / (bike_length + bike_gap)) parallel_capacity = parallel_rows * parallel_columns # 总容量 capacity = slant_capacity + parallel_capacity # 计算便利性得分,假设角度越接近 45 度越便利 convenience = 1 - np.abs(angle - np.pi/4) / (np.pi/2) # 计算综合得分 score = convenience_weight * convenience + capacity_weight * (capacity / (parking_length * parking_width)) scores.append(score) # 更新最佳角度和最佳得分 if score > best_score: best_score = score best_angle = angle print(f"最佳角度 a: {np.degrees(best_angle):.2f} 度") # 可视化综合得分 plt.plot(np.degrees(angles), scores) plt.xlabel('角度 (度)') plt.ylabel('综合得分') plt.title('不同角度的综合得分') plt.axvline(x=np.degrees(best_angle), color='r', linestyle='--', label=f'最佳角度: {np.degrees(best_angle):.2f} 度') plt.legend() plt.show() # 绘制自行车排放示意图 angle_rad = best_angle # 计算自行车的位置 x_positions = [] y_positions = [] slant_width = parking_width * slant_ratio - aisle_width slant_rows = int(slant_width / ((bike_width / np.sin(angle_rad)) + bike_gap)) slant_columns = int(parking_length / ((bike_length / np.cos(angle_rad)) + bike_gap)) for i in range(slant_columns): for j in range(slant_rows): x = i * ((bike_length / np.cos(angle_rad)) + bike_gap) y = j * ((bike_width / np.sin(angle_rad)) + bike_gap) x_positions.append(x) y_positions.append(y) parallel_width = parking_width * (1 - slant_ratio) - aisle_width parallel_rows = int(parallel_width / (bike_width + bike_gap)) parallel_columns = int(parking_length / (bike_length + bike_gap)) for i in range(parallel_columns): for j in range(parallel_rows): x = i * (bike_length + bike_gap) y = slant_width + aisle_width + j * (bike_width + bike_gap) x_positions.append(x) y_positions.append(y) # 绘制自行车 fig, ax = plt.subplots() for x, y in zip(x_positions, y_positions): rect = plt.Rectangle((x, y), bike_length, bike_width, angle=np.degrees(angle_rad), edgecolor='black', facecolor='blue') ax.add_patch(rect) # 设置坐标轴范围和标签 ax.set_xlim(-1, parking_length) ax.set_ylim(-1, parking_width) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_title(f'Bike Arrangement with Optimal Angle: {np.degrees(best_angle):.2f} degrees') # 显示图形 plt.show() 美化输出的图像
09-30
import matplotlib.pyplot as plt import numpy as np import math def calculate_optimal_angle(num_bikes, parking_length, bike_length=2): # 简单示例,根据自行车数量和停车场长度计算最佳角度 # 这里只是一个简单的模拟计算,实际情况会更复杂 angle = math.atan((num_bikes * bike_length) / parking_length) return math.degrees(angle) def draw_bike_arrangement(num_bikes, parking_length, bike_length=2, bike_width=0.5, spacing=0.2): optimal_angle = calculate_optimal_angle(num_bikes, parking_length, bike_length) angle_rad = math.radians(optimal_angle) # 计算自行车的位置 x_positions = [] y_positions = [] for i in range(num_bikes): x = i * (bike_length * math.cos(angle_rad) + spacing) y = i * (bike_length * math.sin(angle_rad)) x_positions.append(x) y_positions.append(y) # 绘制自行车 fig, ax = plt.subplots() for x, y in zip(x_positions, y_positions): rect = plt.Rectangle((x, y), bike_length, bike_width, angle=optimal_angle, edgecolor='black', facecolor='blue') ax.add_patch(rect) # 设置坐标轴范围和标签 ax.set_xlim(-1, parking_length) ax.set_ylim(-1, num_bikes * (bike_length * math.sin(angle_rad) + spacing)) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_title(f'Bike Arrangement with Optimal Angle: {optimal_angle:.2f} degrees') # 显示图形 plt.show() # 示例参数 num_bikes = 10 parking_length = 20 draw_bike_arrangement(num_bikes, parking_length) 与import numpy as np import matplotlib.pyplot as plt # 获取用户输入 bike_width = float(input("请输入自行车宽度(米): ")) bike_length = float(input("请输入自行车长度(米): ")) parking_length = float(input("请输入停车场长度(米): ")) parking_width = float(input("请输入停车场宽度(米): ")) aisle_width = float(input("请输入通道宽度(米): ")) # 定义权重 convenience_weight = 0.6 capacity_weight = 0.4 # 生成不同角度 angles = np.linspace(0, np.pi/2, 100) # 初始化最佳角度和最佳得分 best_angle = 0 best_score = 0 # 存储每个角度的得分 scores = [] # 假设斜放区域占比为 0.5 slant_ratio = 0.5 # 模拟实时占用率(实际应用中应从传感器获取) occupancy_rate = float(input("请输入停车场当前占用率(0 - 1 之间): ")) # 定义不同占用率下的间隙宽度 def get_gap_width(occupancy_rate): if occupancy_rate < 0.2: return 0.3 # 低占用率,大间隙 elif occupancy_rate < 0.8: return 0.2 # 中等占用率,适中间隙 else: return 0.1 # 高占用率,小间隙 bike_gap = get_gap_width(occupancy_rate) for angle in angles: # 避免除以零 if np.isclose(angle, 0): sin_angle = 1e-10 # 避免除以零 cos_angle = 1 elif np.isclose(angle, np.pi/2): sin_angle = 1 cos_angle = 1e-10 # 避免除以零 else: sin_angle = np.sin(angle) cos_angle = np.cos(angle) # 计算斜放区域可停辆数 slant_width = parking_width * slant_ratio - aisle_width slant_rows = int(slant_width / ((bike_width / sin_angle) + bike_gap)) slant_columns = int(parking_length / ((bike_length / cos_angle) + bike_gap)) slant_capacity = slant_rows * slant_columns # 计算平行放区域可停辆数 parallel_width = parking_width * (1 - slant_ratio) - aisle_width parallel_rows = int(parallel_width / (bike_width + bike_gap)) parallel_columns = int(parking_length / (bike_length + bike_gap)) parallel_capacity = parallel_rows * parallel_columns # 总容量 capacity = slant_capacity + parallel_capacity # 计算便利性得分,假设角度越接近 45 度越便利 convenience = 1 - np.abs(angle - np.pi/4) / (np.pi/2) # 计算综合得分 score = convenience_weight * convenience + capacity_weight * (capacity / (parking_length * parking_width)) scores.append(score) # 更新最佳角度和最佳得分 if score > best_score: best_score = score best_angle = angle print(f"最佳角度 a: {np.degrees(best_angle):.2f} 度") # 可视化 plt.plot(np.degrees(angles), scores) plt.xlabel('角度 (度)') plt.ylabel('综合得分') plt.title('不同角度的综合得分') plt.axvline(x=np.degrees(best_angle), color='r', linestyle='--', label=f'最佳角度: {np.degrees(best_angle):.2f} 度') plt.legend() plt.show() 结合
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值