题目链接:点击打开链接
代码:
#include<iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
int n,h;
struct Stack{
int Size;
int top;
int stk[80005];
void Push(int x){
Size ++;
stk[Size] = x;
top = x;
}
void Pop(){
Size --;
top = stk[Size];
}
};
int main(){
Stack st;
st.Size = st.top = 0;
long long ans = 0;
scanf("%d",&n);
for(int i=1; i<=n; i++){
scanf("%d",&h);
if(st.Size == 0){
st.Push(h);
} else {
while(st.top<=h){
if(st.Size == 0) break;
st.Pop();
ans += st.Size;
}
st.Push(h);
}
}
while(true){
if(st.Size == 0) break;
st.Pop();
ans += st.Size;
}
printf("%lld\n",ans);
return 0;
}