单调栈和单调队列

单调栈:进栈元素单调递增(减)的栈,如果碰到比栈顶元素大的元素就进栈,否则不断把栈顶元素弹出直到栈顶元素小于等于要进栈的元素或者栈为空。
应用:
已知一个数列,求每一个元素后面第一个比它大的元素到它的距离,如果没有,输出0。此类问题仅和元素下标差以及元素的值有关,而且问题具有对下标和对值的单调性,可以用这种方法把的算法变成的算法。

#include
#include
using namespace std;
int n;
int a[100010];
int res[100010];
typedef struct {
    int t;
    int pos;
}T;
stack s;
T now, temp;
int main() {
    while (cin >> a[n++]);
    n--;
    for (int i = 0; i < n; i++) {
        now.pos = i;
        now.t = a[i];
        if (s.empty() || s.top().t > a[i]) {
            s.push(now);
        }
        else if (s.top().t == a[i]) continue;
        else {
            while (!s.empty() && s.top().t <= a[i]) {
                temp = s.top();
                s.pop();
                res[temp.pos] = i - temp.pos;
            }
            s.push(now);
        }
    }
    while (!s.empty()) {
        res[s.top().pos] = 0;
        s.pop();
    }
    for (int i = 0; i < n; i++) cout << res[i] << ' ';
    return 0;
}

例题:
最大长方形:http://poj.org/problem?id=2559(原题的网站上不去)
先看最大正方形:https://www.luogu.org/problem/P1387
已知一个0-1矩阵a[M][N],求只包含1的最大正方形的面积。
设为以为右下角的最大正方形边长,易知第一行和第一列的0方格的值为0,1方格的dp值为1。
对于其他的方格,如果它是0方格,dp值为0;如果它是1方格,则考虑它的左边,上边和左上边,如果有任意一个方格为0,则它的dp值为1(只有它自己),否则它的dp值为相邻三个方格dp值的最小值再加1,因为作为最小值min,说明它左上方的方格的左上方至少有一个min*min的正方形,它上方和左方的方格一定能向上和向左扩展出min,再加上它自己,边长为min+1,即:

动态规划即可。

#include
#include
using namespace std;
int h, w;
bool a[1405][1405];
int dp[1405][1405];
int res;
int main() {
    cin >> h >> w;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
            a[i][j] = !a[i][j];
        }
    }
    for (int i = 0; i < h; i++) {
        if (!a[i][0]) dp[i][0] = 1;
    }
    for (int j = 0; j < w; j++) {
        if (!a[0][j]) dp[0][j] = 1;
    }
    for (int i = 1; i < h; i++) {
        for (int j = 1; j < w; j++) {
            if (a[i][j]) dp[i][j] = 0;
            else {
                dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
                res = max(res, dp[i][j]);
            }
        }
    }
    cout << res;
    return 0;
}

时间复杂度:。
最大长方形:把上题的正方形改成长方形。
动态规划不对了,因为长方形长宽不一样。
最朴素的想法:枚举所有点,找它们能构成的最大长方形面积的最大值。联想关灯问题,可以按顺序做,枚举每一行以为底的高最短的长方形面积,对于高可以用预处理算出来,即从上往下算最多能连续多少个1,显然有,用动态规划容易算得,问题转化为求直方图上的最大长方形面积,预处理的时间复杂度为。
然后遍历每一行,每一行再遍历每一组,去最小高乘宽度再取最大值,时间复杂度为。
下面用单调栈把复杂度降到:
栈储存直方图上长方形的高和左端的位置pos。
对每一行,从左往右枚举元素:
若栈为空或者栈顶元素的高小于要入栈元素的高,就入栈。
否则出栈到栈顶元素的高小于要入栈元素的高为止,设置pos为最后一个出栈元素的pos,入栈,在这个过程中,对于每一个出栈的元素,都计算它和已出栈元素所能形成的最大长方形的面积:,直到所有元素都入过栈,把栈内的元素清空。
这样实际上每一行只遍历了一遍,时间复杂度为。
部分代码(求柱状图的最大矩形,能过poj2559):

#pragma warning(disable:4996)
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 100010;
int n;
ll a[maxn];
ll res;
typedef struct {
    ll pos;
    ll h;
}rect;
stack s;
int main() {
    rect now, temp;
    while (1) {
        scanf("%d", &n);
        if (n == 0) break;
        for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
        res = 0;
        a[n] = -1;
        for (int i = 0; i <= n; i++) {
            now.pos = i;
            now.h = a[i];
            if (s.empty() || a[i] > s.top().h) {
                s.push(now);
            }
            else if (a[i] == s.top().h) continue;
            else {
                temp.pos = i;
                while (!s.empty() && a[i] <= s.top().h) {
                    temp = s.top();
                    s.pop();
                    res = max(res,ll((i - temp.pos)*temp.h));
                }
                now.pos = temp.pos;
                s.push(now);
            }
        }
        printf("%I64d\n", res);
    }

    return 0;
}

单调队列:https://www.luogu.org/problem/P1886
连续移动区间的最值问题。
首先想到可以用树状数组,时间复杂度为,似乎很好,但是此题,,这样还是会超时。
1s的时间限制,对应及以上,对应,对应。
用单调队列可以把时间复杂度降到。
如果队列为空,从队尾进队;否则比较要进队的元素和队尾元素,如果它比队尾元素大,就进队,否则出队直到队尾元素比它小,进队。如果要进队的元素和队首元素下标差大于等于,队首元素从队首出队(相当于滑动)。
STL实现:

#include
#include
using namespace std;
int n, k;
const int maxn = 1000010;
struct S {
    int pos;
    int val;
}a[maxn];
deque q;
int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].val;
        a[i].pos = i;
    }
    for (int i = 1; i <= n; i++) {
        while (!q.empty() && q.back().val >= a[i].val) q.pop_back();
        q.push_back(a[i]);
        if (a[i].pos - q.front().pos >= k) q.pop_front();
        if (i >= k) cout << q.front().val << ' ';
    }
    cout << endl;
    q.clear();
    for (int i = 1; i <= n; i++) {
        while (!q.empty() && q.back().val <= a[i].val) q.pop_back();
        q.push_back(a[i]);
        if (a[i].pos - q.front().pos >= k) q.pop_front();
        if (i >= k) cout << q.front().val << ' ';
    }
    return 0;
}

数组实现:一定要记住设tail=0,head=1,判断非空的条件是head<=tail,不这样的话会出现++tail首元素没读,tail++从队首出队错误。

#include
using namespace std;
const int maxn = 1000010;
int n, k;
struct S {
    int pos;
    int val;
}a[maxn], q[maxn];
int head, tail;
int main() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].val;
        a[i].pos = i;
    }
    head = 1;
    for (int i = 1; i <= n; i++) {
        while (head <= tail && q[tail].val >= a[i].val) tail--;
        q[++tail] = a[i];//注意是++tail,不是tail++
        if (a[i].pos - q[head].pos >= k) head++;
        if (i >= k) cout << q[head].val << ' ';
    }
    cout << endl;
    head = 1;
    tail = 0;
    for (int i = 1; i <= n; i++) {
        while (head <= tail && q[tail].val <= a[i].val) tail--;
        q[++tail] = a[i];
        if (a[i].pos - q[head].pos >= k) head++;
        if (i >= k) cout << q[head].val << ' ';
    }
    return 0;
}

你可能感兴趣的:(单调栈和单调队列)