POJ3250【单调栈】

思路:
维护一个单调递增的栈,对于栈顶元素<=新值来说,那么后面的,我一定看不到了,pop掉以后,那么这时候的栈的大小就是我能看到的这个刚刚pop出去元素的个数。

//#include <bits/stdc++.h>
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;

typedef long long LL;
const int N=1e5+10;

int main()
{
    stack<int>q;
    while(!q.empty())
        q.pop();
    LL sum=0;
    int n,t;
    scanf("%d",&n);
    scanf("%d",&t);
    q.push(t);
    for(int i=1;i<n;i++)
    {
        scanf("%d",&t);
        while(!q.empty()&&t>=q.top())
            q.pop();
        sum+=q.size();
        q.push(t);
    }
    printf("%lld
",sum);
    return 0;
}
原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934811.html