单调栈

· · 个人记录

单调栈:

就是用于找左边或者右边第一个大于或小于他的元素位置 。

性质:

  1. 首先跟栈一样先进后出 。

  2. 然后栈里面都是没有找到满足要求的元素的元素的下标 。

  3. 栈中的元素值有单调性 ,越靠近栈第 ,元素就会越(要求)。

    P2947 [USACO09MAR] Look Up S

    思路:

    是道模版题思路没啥好说的 。

    代码:

    
    #include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

int n, a[N], w[N], r[N], l[N]; stack<int> stk;

int main() { cin >> n;

for(int i = 1; i <= n; i++)
{
    cin >> a[i] >> w[i];
}

for(int i = 1; i <= n; i++)
{ 
    while(stk.empty() == 0 && a[stk.top()] < a[i])
    { 
        r[i] += w[stk.top()]; 
        stk.pop();
    }

    stk.push(i);
}

while(stk.empty() == 0)
{
    stk.pop();
}

for(int i = n; i >= 1; i--)
{ 
    while(stk.empty() == 0 && a[stk.top()] < a[i])
    { 
        l[i] += w[stk.top()];
        stk.pop();
    }

    stk.push(i);
}

int maxi = -1e9;

for(int i = 1; i <= n; i++)
{
    maxi = max(maxi, l[i] + r[i]);
}

cout << maxi;
return 0;

}

# [P1901 发射站](https://www.luogu.com.cn/problem/P1901)
## 思路:
### 我们只需要要转换成让栈顶的元素接受就好了 。
## 代码:
```cpp
#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

int n, a[N], w[N], r[N], l[N];
stack<int> stk;

int main()
{
    cin >> n;

    for(int i = 1; i <= n; i++)
    {
        cin >> a[i] >> w[i];
    }

    for(int i = 1; i <= n; i++)
    { 
        while(stk.empty() == 0 && a[stk.top()] < a[i])
        { 
            r[i] += w[stk.top()]; 
            stk.pop();
        }

        stk.push(i);
    }

    while(stk.empty() == 0)
    {
        stk.pop();
    }

    for(int i = n; i >= 1; i--)
    { 
        while(stk.empty() == 0 && a[stk.top()] < a[i])
        { 
            l[i] += w[stk.top()];
            stk.pop();
        }

        stk.push(i);
    }

    int maxi = -1e9;

    for(int i = 1; i <= n; i++)
    {
        maxi = max(maxi, l[i] + r[i]);
    }

    cout << maxi;
    return 0;
}

P2866 [USACO06NOV] Bad Hair Day S

思路:

因为可以看到牛的总和就是可以被牛看到的总和 ,所以答案就是每次加上栈中元素的个数就好了 。

代码:

#include<bits/stdc++.h>

#define int long long

using namespace std; 

int n;
stack<int> stk;

signed main()
{
    cin >> n;

    int ans = 0;

    for(int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;

        while(stk.empty() == 0 && x >= stk.top())
        {
            stk.pop();
        }

        ans += stk.size();

        stk.push(x);
    }

    cout << ans;
    return 0;
}

P2422 良好的感觉

思路:

既然区间不好找最小值那我们就可以用最小值来找区间 。先求个前缀和 ,让后给每个数都找第一个右边大于他的数 ,和左边第一个小于他的数 ,如果没找到就标记为 n+1 和 0 。最后取最大值 。

代码:

#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 1e5 + 5;

int n, a[N], pre[N], r[N], l[N];
stack<int> stk;

signed main()
{
    cin >> n;

    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];    
        pre[i] = pre[i - 1] + a[i];
    } 

    for(int i = 1; i <= n; i++)
    {
        while(stk.empty() == 0 && a[i] < a[stk.top()])
        {
            r[stk.top()] = i;
            stk.pop();
        }

        stk.push(i);
    }

    while(stk.empty() == 0)
    {
        r[stk.top()] = n + 1;
        stk.pop();
    }

    for(int i = n; i >= 1; i--)
    {
        while(stk.empty() == 0 && a[i] < a[stk.top()])
        {
            l[stk.top()] = i;
            stk.pop();
        }

        stk.push(i);
    }

    while(stk.empty() == 0)
    {
        l[stk.top()] = 0;
        stk.pop();
    }

    int maxi = 0;

    for(int i = 1; i <= n; i++)
    {
        int o = pre[r[i] - 1] - pre[l[i]];
        maxi = max(maxi, o * a[i]);
    }

    cout << maxi;
    return 0;
}