首页 > 其他 > 详细

题解【洛谷P5788】【模板】单调栈

时间:2020-01-31 22:01:35      阅读:99      评论:0      收藏:0      [点我收藏+]

题面

单调栈模板题。

单调栈与单调队列一样,都是维护了一段区间内的顺序。

然后……这个题用一个栈维护一下贪心就没了。

具体参考这一篇题解

#include <bits/stdc++.h>
#define itn int
#define gI gi

using namespace std;

inline int gi()
{
    int f = 1, x = 0; char c = getchar();
    while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return f * x;
}

const int maxn = 3000003;

int n, a[maxn], sta[maxn], ans[maxn], topp;

int main()
{
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    n = gi();
    for (int i = 1; i <= n; i+=1) a[i] = gi();
    for (int i = n; i >= 1; i-=1)
    {
        while (topp && a[sta[topp]] <= a[i]) --topp;
        ans[i] = sta[topp];
        sta[++topp] = i;
    }
    for (int i = 1; i <= n; i+=1) printf("%d ", ans[i]);
    return 0;
}

题解【洛谷P5788】【模板】单调栈

原文:https://www.cnblogs.com/xsl19/p/12246865.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!