跳到正文

题解

题解:P1034 [NOIP 2002 提高组] 矩形覆盖

hugh_foshan约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年4月19日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 3 分钟
获赞
1
题目
P1034

版本

  1. v1当前

题意

平面上有 n 个点,将其包含在 k 个矩形中(不相交),求矩形的最小面积和。

思路放代码里了,懒得写。

AC Code

C++
#include<iostream>//头文件
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
struct point {//点的坐标
    int x, y;
}a[60];
int cmp(point a, point b) {//定义排序函数
    if (a.x == b.x)return a.y < b.y;
    return a.x < b.x;
}
struct block {//矩阵坐标
    int x1, y1, x2, y2;
}b[5];
int n, k, ans = 1e9;
void DFS(int pos, int cnt, int smm) {//pos表示要覆盖的点,cnt表示第几个矩阵, smm表示所需面积。
    if (pos > n) {//更新最小面积
        ans = min(ans, smm);
        return;
    }
    if (cnt > k)return;//剪枝
    b[cnt].x1 = a[pos].x;//更新矩阵坐标
    b[cnt].x2 = a[pos].x;
    b[cnt].y1 = a[pos].y;
    b[cnt].y2 = a[pos].y;
    for (int i = pos; i <= n; i++) {
        b[cnt].y2 = max(b[cnt].y2, a[i].y);//更改矩阵坐标
        b[cnt].x2 = max(b[cnt].x2, a[i].x);
        b[cnt].x1 = min(b[cnt].x1, a[i].x);
        b[cnt].y1 = min(b[cnt].y1, a[i].y);
        for (int j = 1; j < cnt; j++) {
            if (b[cnt].x1 <= b[j].x2 && b[cnt].y1 <= b[j].y2)return;
        }
        if (i < n && cnt == k)continue;//剪枝++
        DFS(i + 1, cnt + 1, smm + (b[cnt].x2 - b[cnt].x1) * (b[cnt].y2 - b[cnt].y1));//继续深搜
    }
    return;
}
int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].y >> a[i].x;
    }
    sort(a + 1, a + n + 1, cmp);//排序
    memset(b, 0, sizeof b);
    DFS(1, 1, 0);//开始深搜
    printf("%d\n", ans);//输出
    return 0;//完结撒花QAQ
}

管理员求过!!

原文
存档评论1 条抓取于