[Luogu] 矩形覆盖

https://www.luogu.org/problemnew/show/P1034

数据太水

爆搜过掉

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

const int N = 55;

int n, K, Use[N], A[N], Answer = 1e7;
int Maxx, Minx, Maxy, Miny;
struct Node {int X, Y;} Point[N];

#define gc getchar()

inline int read() {
    int x = 0; char c = gc;
    while(c < '0' || c > '9') c = gc;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
    return x;
}

void Out() {
    int now_ans = 0;
    Maxx = Maxy = 0;
    Minx = Miny = 1e7;
    for(int i = 1; i <= K; i ++) {
        for(int j = A[i - 1] + 1; j <= A[i]; j ++) {
            Maxx = std:: max(Maxx, Point[j].X);
            Maxy = std:: max(Maxy, Point[j].Y);
            Minx = std:: min(Minx, Point[j].X);
            Miny = std:: min(Miny, Point[j].Y); 
        }
        now_ans += (Maxx - Minx) * (Maxy - Miny);
        Maxx = Maxy = 0;
        Minx = Miny = 1e7;
    }
    if(now_ans < Answer) Answer = now_ans;
}

bool Cmp1(Node a, Node b) {
    if(a.X == b.X) return a.Y < b.Y;
    return a.X < b.X;
} 
bool Cmp2(Node a, Node b) {
    if(a.Y == b.Y) return a.X < b.X; 
    return a.Y < b.Y;
}

void Dfs(int tot) {
    if(tot == K) {Out(); return ;}
    for(int i = A[tot - 1] + 1; i <= n; i ++) {
        if(!Use[i]) {
            Use[i] = 1;
            A[tot] = i;
            Dfs(tot + 1);
            Use[i] = 0;
        }
    }
}

int main() {
    n = read(), K = read();
    for(int i = 1; i <= n; i ++) Point[i].X = read(), Point[i].Y = read();
    std:: sort(Point + 1, Point + n + 1, Cmp1);
    A[K] = n;
    Dfs(1);
    std:: sort(Point + 1, Point + n + 1, Cmp2);
    Dfs(1);
    std:: cout << Answer;
    return 0;
}

 

posted @ 2018-05-22 17:32  xayata  阅读(169)  评论(0编辑  收藏  举报