• P2216 [HAOI2007]理想的正方形


    题目描述

    有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小。

    输入格式

    第一行为3个整数,分别表示a,b,n的值

    第二行至第a+1行每行为b个非负整数,表示矩阵中相应位置上的数。每行相邻两数之间用一空格分隔。

    输出格式

    仅一个整数,为a*b矩阵中所有“n*n正方形区域中的最大整数和最小整数的差值”的最小值。

    输入输出样例

    输入 #1
    5 4 2
    1 2 5 6
    0 17 16 0
    16 17 2 1
    2 10 2 1
    1 2 2 2
    
    输出 #1
    1

    说明/提示

    问题规模

    (1)矩阵中的所有数都不超过1,000,000,000

    (2)20%的数据2<=a,b<=100,n<=a,n<=b,n<=10

    (3)100%的数据2<=a,b<=1000,n<=a,n<=b,n<=100

     单调队列纵横处理

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(ll  i=a;i<=b;i++)
    #define dec(i,a,b)      for(ll  i=a;i>=b;i--)
    #define forn(i, n)      for(ll i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod = 998244353;
    const int N = 2e6 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    inline int qpow(int x, ll n) {
        int r = 1;
        while (n > 0) {
            if (n & 1) r = 1ll * r * x % mod;
            n >>= 1; x = 1ll * x * x % mod;
        }
        return r;
    }
    inline int add(int x, int y) {
        return ((x % mod) + (y % mod)) % mod;
    }
    inline int sub(int x, int y) {
        x -= y;
        return x < 0 ? x += mod : x;
    }
    inline int mul(int x, int y) {
        return (1ll * (x % mod) * (y % mod)) % mod;
    }
    inline int inv(int x) {
        return qpow(x, mod - 2);
    }
    
    class incque
    {
        deque<int>q;
    public:
        void push(int n)
        {
            while (!q.empty() && n > q.back())
                q.pop_back();
            q.push_back(n);
        }
        int max()
        {
            return q.front();
        }
        void pop(int n)
        {
            if (!q.empty() && q.front() == n)
                q.pop_front();
        }
    };
    
    class decque
    {
        deque<int>q;
    public:
        void push(int n)
        {
            while (!q.empty() && n < q.back())
                q.pop_back();
            q.push_back(n);
        }
        int min()
        {
            return q.front();
        }
        void pop(int n)
        {
            if (!q.empty() && q.front() == n)
                q.pop_front();
        }
    };
    int maxx[1005][1005], minx[1005][1005], maxy[1005][1005], miny[1005][1005];
    int main()
    {
        int a, b, n;
        cin >> a >> b >> n;
        vector<vector<int> > g(a + 1, vector<int>(b + 1));
        rep(i, 1, a)
        {
            rep(j, 1, b)
            {
                g[i][j]=read();
            }
        }
        rep(i, 1, a)
        {
            incque q1;
            decque q0;
            rep(j, 1, b)
            {
                if (j < n)
                {
                    q1.push(g[i][j]);
                    q0.push(g[i][j]);
                }
                else
                {
                    q1.push(g[i][j]);
                    maxx[i][j-n+1] = q1.max();
                    q1.pop(g[i][j-n+1]);
                    q0.push(g[i][j]);
                    minx[i][j - n + 1] = q0.min();
                    q0.pop(g[i][j - n + 1]);
                }
            }
        }
        rep(i, 1, b - n + 1)
        {
            incque q1;
            decque q0;
            rep(j, 1,a)
            {
                if (j < n)
                {
                    q1.push(maxx[j][i]);
                    q0.push(minx[j][i]);
                }
                else
                {
                    q1.push(maxx[j][i]);
                    maxy[j - n + 1][i] = q1.max();
                    q1.pop(maxx[j - n + 1][i]);
                    q0.push(minx[j][i]);
                    miny[j - n + 1][i] = q0.min();
                    q0.pop(minx[j - n + 1][i]);
                }
            }
        }
        int res = inf;
        rep(i, 1, a - n + 1)
        {
            rep(j, 1, b - n + 1)
            {
                res = min(res, maxy[i][j] - miny[i][j]);
            }
        }
        cout << res << endl;
        return 0;
    }
  • 相关阅读:
    SQL注入: with rollup特性
    【转】kali配置--修改IP和DNS
    【转】getopt模块,实现获取命令行参数
    socket编程: TypeError: must be bytes or buffer, not str
    Ansible进阶之企业级应用
    Ansible之Playbook详解
    Ansible之常用模块介绍
    JAVA企业级应用Tomcat实战
    ubuntu网络、包管理、工作内容小结
    shell细节决定高度
  • 原文地址:https://www.cnblogs.com/dealer/p/13305988.html
Copyright © 2020-2023  润新知