比赛 2024暑假C班集训9 评测结果 AAWWWWW
题目名称 矩形覆盖 最终得分 29
用户昵称 彭欣越 运行时间 0.254 s
代码语言 C++ 内存使用 0.82 MiB
提交时间 2024-07-09 11:47:45
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll res=1e9+10,res1=1e9+10;
int n,k,mk[55];
struct node {
    int x1,x2,y1,y2;
}s[5];
struct edge {
    int x,y;
}a[55];
bool cmp1 (edge x,edge y) {
    if (x.y==y.y) return x.x<y.x;
    return x.y<y.y;
}
bool cmp2 (edge x,edge y) {
    if (x.x==y.x) return x.y<y.y;
    return x.x<y.x;
}
void dfs (int idx,ll ans,int x,int y) {
    if (idx==1) {
        int b1=0,a1=510,d1=0,c1=510;
        for (int i=1;i<=n;i++) {
            if (mk[i]>=1) continue;
            a1=min(a[i].x,a1);
            b1=max(a[i].x,b1);
            c1=min(a[i].y,c1);
            d1=max(a[i].y,d1);
        }
        res=min(res,ans+(b1-a1)*(d1-c1));
        return;
    }
    for (int i=x;i<=x;i++) {
        for (int j=i;j<=n;j++) {
            if (mk[i]>=1||mk[j]>=1) continue;
            if (i==1&&j==n) continue;
            int b1=0,a1=510,d1=0,c1=510;
            a1=min(a[i].x,a1);
            b1=max(a[i].x,b1);
            c1=min(a[i].y,c1);
            d1=max(a[i].y,d1);
            a1=min(a[j].x,a1);
            b1=max(a[j].x,b1);
            c1=min(a[j].y,c1);
            d1=max(a[j].y,d1);
            if (ans+(b1-a1)*(d1-c1)>=res) continue;
            mk[i]=1,mk[j]=1;
            for (int k=1;k<=n;k++) {
                if (a[k].x>=a1&&a[k].x<=b1&&a[k].y>=c1&&a[k].y<=d1) mk[k]++;
            }
            dfs(idx-1,ans+(b1-a1)*(d1-c1),j+1,y);
            mk[i]--,mk[j]--;
            for (int k=1;k<=n;k++) {
                if (a[k].x>=a1&&a[k].x<=b1&&a[k].y>=c1&&a[k].y<=d1) mk[k]--;
            }
        }
    }
    for (int i=y;i<=y;i++) {
        for (int j=i;j>=1;j--) {
            if (mk[i]>=1||mk[j]>=1) continue;
            if (i==n&&j==1) continue;
            int b1=0,a1=510,d1=0,c1=510;
            a1=min(a[i].x,a1);
            b1=max(a[i].x,b1);
            c1=min(a[i].y,c1);
            d1=max(a[i].y,d1);
            a1=min(a[j].x,a1);
            b1=max(a[j].x,b1);
            c1=min(a[j].y,c1);
            d1=max(a[j].y,d1);
            if (ans+(b1-a1)*(d1-c1)>=res) continue;
            mk[i]=1,mk[j]=1;
            for (int k=1;k<=n;k++) {
                if (a[k].x>=a1&&a[k].x<=b1&&a[k].y>=c1&&a[k].y<=d1) mk[k]++;
            }
            dfs(idx-1,ans+(b1-a1)*(d1-c1),x,j-1);
            mk[i]--,mk[j]--;
            for (int k=1;k<=n;k++) {
                if (a[k].x>=a1&&a[k].x<=b1&&a[k].y>=c1&&a[k].y<=d1) mk[k]--;
            }
        }
    }
}
int main () {
    freopen("jxfg.in","r",stdin);
    freopen("jxfg.out","w",stdout);
    cin >> n >> k;
    for (int i=1;i<=n;i++) cin >> a[i].x >> a[i].y;
    if (k==1) {
        int b1=0,a1=510,d1=0,c1=510;
        for (int i=1;i<=n;i++) {
            a1=min(a[i].x,a1);
            b1=max(a[i].x,b1);
            c1=min(a[i].y,c1);
            d1=max(a[i].y,d1);
        }
        cout << (b1-a1)*(d1-c1) <<endl;
    }else{
        sort(a+1,a+1+n,cmp1);
        dfs(k,0,1,n);
        sort(a+1,a+1+n,cmp2);
        dfs(k,0,1,n);
        cout << res <<endl;
    }
    return 0;
}