码迷,mamicode.com
首页 > 其他好文 > 详细

[Luogu] 矩形覆盖

时间:2018-05-22 18:28:40      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:get   char   getchar   clu   while   out   #define   use   line   

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;
}

 

[Luogu] 矩形覆盖

标签:get   char   getchar   clu   while   out   #define   use   line   

原文地址:https://www.cnblogs.com/shandongs1/p/9073171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!