zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    1250. Check If It Is a Good Array
    380. Insert Delete GetRandom O(1)
    378. Kth Smallest Element in a Sorted Matrix
    341. Flatten Nested List Iterator
    387. First Unique Character in a String
    454. 4Sum II
    D
    勇敢的妞妞 ( 状压 + 思维)
    P1879 [USACO06NOV]玉米田Corn Fields (状压dp入门)
    G
  • 原文地址:https://www.cnblogs.com/shandongs1/p/9073171.html
Copyright © 2011-2022 走看看