ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

AcWing 算法提高课 二维单调队列优化dp

2022-07-03 21:07:30  阅读:184  来源: 互联网

标签:min 队列 back 1111 int que row dp AcWing


单调队列可以求出,区间内的最值。

对于二维的情况,可以先在每一行,用单调队列求出,行方向上的最值。

然后在行方向上的最值的基础上,在每一列,用单调队列求出列方向上的最值。

即可得到二维区间的最值。

例题:

  1091. 理想的正方形 代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int num[1111][1111];
int row_max[1111][1111];
int row_min[1111][1111];
int col_max[1111][1111];
int col_min[1111][1111];
void YD()
{
    int m, n, sz;
    cin >> m >> n >> sz;
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++)
            cin >> num[i][j];
    for (int i = 1; i <= m; i++)
    {
        deque<int> que;
        row_max[i][1] = row_min[i][1] = num[i][1];
        que.push_back(1);
        for (int j = 2; j <= n; j++)
        {
            if (que.front() <= j - sz)
                que.pop_front();
            while (que.size()&&num[i][que.back()] <= num[i][j])
                que.pop_back();
            que.push_back(j);
            row_max[i][j] = num[i][que.front()];
        }
        que.clear();
        que.push_back(1);
        for (int j = 2; j <= n; j++)
        {
            if (que.front() <= j - sz)
                que.pop_front();
            while (que.size() && num[i][que.back()] >= num[i][j])
                que.pop_back();
            que.push_back(j);
            row_min[i][j] = num[i][que.front()];
        }

    }
    for (int j = 1; j <= n; j++)
    {
        deque<int> que;
        col_max[1][j] = row_max[1][j];
        que.push_back(1);
        for (int i = 2; i <= m; i++)
        {
            if (que.front() <= i - sz)
                que.pop_front();
            while (que.size() && row_max[que.back()][j] <= row_max[i][j])
                que.pop_back();
            que.push_back(i);
            col_max[i][j] = row_max[que.front()][j];
        }
        que.clear();
        que.push_back(1);
        col_min[1][j] = row_min[1][j];
        for (int i = 2; i <= m; i++)
        {
            if (que.front() <= i - sz)
                que.pop_front();
            while (que.size() && row_min[que.back()][j] >= row_min[i][j])
                que.pop_back();
            que.push_back(i);
            col_min[i][j] = row_min[que.front()][j];
        }

    }
    int res = 2e9;
    for (int i = sz; i <= m; i++)
    {
        for (int j = sz; j <= n; j++)
        {
            res = min(res, col_max[i][j] - col_min[i][j]);
        }
    }
    cout << res << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T = 1;
    //cin >> T;
    while (T--)
    {
        YD();
    }
    return 0;
}
View Code

 

标签:min,队列,back,1111,int,que,row,dp,AcWing
来源: https://www.cnblogs.com/ydUESTC/p/16440876.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有