poj3250(单调栈)

传送门

ac代码:

#include<bits/stdc++.h>
#define per(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
//#define int long long
const ll inf =2333333333333333LL;
const double eps=1e-8;
int read(){
    char ch=getchar();
    int res=0,f=0;
    while(ch<'0' || ch>'9'){f=(ch=='-'?-1:1);ch=getchar();}
    while(ch>='0'&&ch<='9'){res=res*10+(ch-'0');ch=getchar();}
    return res*f;
}
// ------------------------head
#define mod 1000000007
const int N=80005;
int n,h[N],res[N];
stack<int>st;

signed main()
{
    while(!st.empty())st.pop();
    scanf("%d",&n);
    per(i,1,n)scanf("%d",&h[i]);
    for(int i=n;i>0;i--){
        while(!st.empty()&&h[i]>h[st.top()])st.pop();
        if(st.empty())res[i]=n-i;
        else res[i]=st.top()-1-i;
        st.push(i);
    }
    ll ans=0;
    per(i,1,n)ans+=(ll)res[i];
    printf("%lld\n",ans);
    return 0;
}

 

posted @ 2018-09-17 12:56  WindFreedom  阅读(372)  评论(0)    收藏  举报