比赛 201712练习 评测结果 AAAAAAA
题目名称 矩形覆盖 最终得分 100
用户昵称 胡嘉兴 运行时间 0.045 s
代码语言 C++ 内存使用 0.31 MiB
提交时间 2017-12-26 17:21:18
显示代码纯文本
#include<bits/stdc++.h>
#define N 57
using namespace std;
struct point
{
    int x;
    int y;
    bool operator < (const point& a)const{
        if(y!=a.y)
        {
            return x < a.x;
        }
        return y < a.y;
    }
}s[N];
struct rec
{
    int u;
    int d;
    int l;
    int r;
    int check()
    {
        return (r-l)*(u-d);
    }
    void hold(int x, int y)
    {
        u = max(u, y);
        d = min(d, y);
        l = min(l, x);
        r = max(r, x);
        return;
    }
    void init()
    {
        u = r = -1000;
        d = l = 1000;
        return;
    }
}ans[4];
int n, k, cnt = 0x3f3f3f3f;
inline bool intersect(rec a, rec b)
{
    return ((b.l>=a.l&&b.l<=a.r)||(b.r>=a.l&&b.r<=a.r))&&((b.d>=a.d&&b.d<=a.u)||(b.u>=a.d&&b.u<=a.u));
}
void dfs(int num)
{
    int sum = 0;
    for(int i = 0; i < k; i++)
    {
        for(int j = i+1; j < k; j++)
        {
            if(ans[j].r<0)
            {
                break;
            }
            if(intersect(ans[i], ans[j]))
            {
                return;
            }
        }
    }
    for(int i = 0; i < k; i++)
    {
        if(ans[i].r<0)
        {
            if(!i)
            {
                sum = 1000;
            }
            break;
        }
        sum += ans[i].check();
    }
    if(sum>=cnt)
    {
        return;
    }
    if(num>n)
    {
        cnt = sum;
        return;
    }
    for(int i = 0; i < k; i++)
    {
        rec t = ans[i];
        ans[i].hold(s[num].x, s[num].y);
        dfs(num+1);
        ans[i] = t;
        if(t.r<0)
        {
            break;
        }
    }
    return;
}
int main()
{
    freopen("jxfg.in","r",stdin);
	freopen("jxfg.out","w",stdout);
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d%d", &s[i].x, &s[i].y);
    }
    for(int i = 0; i < k; i++)
    {
        ans[i].init();
    }
    dfs(1);
    printf("%d\n", cnt);
}