zoukankan      html  css  js  c++  java
  • bzoj 1047 : [HAOI2007]理想的正方形 单调队列dp

    题目链接

    1047: [HAOI2007]理想的正方形

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 2369  Solved: 1266
    [Submit][Status][Discuss]

    Description

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

    Input

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

    Output

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

    Sample Input

    5 4 2
    1 2 5 6
    0 17 16 0
    16 17 2 1
    2 10 2 1
    1 2 2 2

    Sample Output

    1
     
    用单调队列, 比如求最大值的时候, 先求出每一行以i结尾的长度为n的最大值, 保存在tmp数组里面, 然后利用tmp数组求出每一列以j结尾的长度为n的最大值, 这个值就相当于以i, j为右下角的一个正方形的最大值。
     
    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    int n, m, k;
    const int maxn = 1005;
    int a[maxn][maxn], f[maxn][maxn], g[maxn][maxn], tmp[maxn][maxn];
    deque <int> q;
    void get_max() {
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=m; j++) {
                tmp[i][j] = a[i][j];
                while(!q.empty()&&j-q.front()+1>k)
                    q.pop_front();
                if(!q.empty()) {
                    tmp[i][j] = max(tmp[i][j], a[i][q.front()]);
                }
                while(!q.empty() && a[i][j]>a[i][q.back()]) {
                    q.pop_back();
                }
                q.push_back(j);
            }
            while(!q.empty())
                q.pop_back();
        }
        for(int j = 1; j<=m; j++) {
            for(int i = 1; i<=n; i++) {
                f[i][j] = tmp[i][j];
                while(!q.empty()&&i-q.front()+1>k)
                    q.pop_front();
                if(!q.empty()) {
                    f[i][j] = max(f[i][j], tmp[q.front()][j]);
                }
                while(!q.empty() && tmp[i][j]>tmp[q.back()][j])
                    q.pop_back();
                q.push_back(i);
            }
            while(!q.empty())
                q.pop_back();
        }
    }
    void get_min() {
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=m; j++) {
                tmp[i][j] = a[i][j];
                while(!q.empty()&&j-q.front()+1>k)
                    q.pop_front();
                if(!q.empty()) {
                    tmp[i][j] = min(tmp[i][j], a[i][q.front()]);
                }
                while(!q.empty() && a[i][j]<a[i][q.back()]) {
                    q.pop_back();
                }
                q.push_back(j);
            }
            while(!q.empty())
                q.pop_back();
        }
        for(int j = 1; j<=m; j++) {
            for(int i = 1; i<=n; i++) {
                g[i][j] = tmp[i][j];
                while(!q.empty()&&i-q.front()+1>k)
                    q.pop_front();
                if(!q.empty()) {
                    g[i][j] = min(g[i][j], tmp[q.front()][j]);
                }
                while(!q.empty() && tmp[i][j]<tmp[q.back()][j])
                    q.pop_back();
                q.push_back(i);
            }
            while(!q.empty())
                q.pop_back();
        }
    }
    int main()
    {
        cin>>n>>m>>k;
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=m; j++) {
                scanf("%d", &a[i][j]);
            }
        }
        get_max();
        get_min();
        int ans = 2e9+5;
        for(int i = 1; i<=n; i++) {
            for(int j = 1; j<=m; j++) {
                if(i<k||j<k)
                    continue;
                ans = min(ans, f[i][j]-g[i][j]);
            }
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    qt捕鱼达人
    linux通过源码安装gdb
    打造简易http服务器
    linux缓存同步
    NOI之后的碎碎念
    Validate至少有一个不能为空
    IOI 2020 集训队作业
    P6033 [NOIP2004 提高组] 合并果子 加强版 题解
    P3017 [USACO11MAR]Brownie Slicing G 题解
    LOJ #145. DFS 序 2 题解
  • 原文地址:https://www.cnblogs.com/yohaha/p/5224474.html
Copyright © 2011-2022 走看看