提交时间:2026-07-17 12:31:29
运行 ID: 92668
#include<bits/stdc++.h> using namespace std; struct Point { int x, y; } p[55]; struct Rect { int x1, x2, y1, y2; Rect() { x1 = y1 = 501; x2 = y2 = -1; } }; int n, k, ans = 1e9; Rect r[5]; int area(Rect a) { if (a.x1 > a.x2 || a.y1 > a.y2) return 0; return (a.x2 - a.x1) * (a.y2 - a.y1); } bool overlap(Rect a, Rect b) { if (a.x1 > a.x2 || a.y1 > a.y2 || b.x1 > b.x2 || b.y1 > b.y2) return false; if (a.x2 < b.x1 || b.x2 < a.x1 || a.y2 < b.y1 || b.y2 < a.y1) return false; return true; } void dfs(int idx, int sum) { if (sum >= ans) return; if (idx > n) { ans = min(ans, sum); return; } for (int i = 0; i < k; i++) { if (r[i].x1 > r[i].x2) { bool hasEmpty = false; for (int j = 0; j < i; j++) if (r[j].x1 > r[j].x2) { hasEmpty = true; break; } if (hasEmpty) continue; } Rect tmp = r[i]; r[i].x1 = min(r[i].x1, p[idx].x); r[i].x2 = max(r[i].x2, p[idx].x); r[i].y1 = min(r[i].y1, p[idx].y); r[i].y2 = max(r[i].y2, p[idx].y); bool flag = true; for (int j = 0; j < k; j++) if (i != j && overlap(r[i], r[j])) { flag = false; break; } if (flag)dfs(idx + 1, sum + area(r[i]) - area(tmp)); r[i] = tmp; } } int main() { cin >> n >> k; for (int i = 1; i <= n; i++)cin >> p[i].x >> p[i].y; dfs(1, 0); cout << ans << endl; return 0; //完结撒花! //By Rainmount }