只需要向前统计,不需要向后统计,比前两道题还简单些。
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
struct Node
{
int l;
long long h;
};
int main()
{
int n;
scanf("%d", &n);
long long sum = 0;
stack <Node> S;
Node temp, tmp;
for(int i = 0; i < n; i++)
{
scanf("%lld", &temp.h);
temp.l = i;
while(!S.empty() && S.top().h <= temp.h)
{
tmp = S.top();
S.pop();
sum += i - tmp.l - 1;
//temp.l = tmp.l;
}
S.push(temp);
}
while(!S.empty())
{
tmp = S.top();
S.pop();
sum += n - tmp.l - 1;
}
printf("%lld\n", sum);
return 0;
}