信息
- ID
- 2588
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 4
- 标签
- 递交数
- 5
- 已通过
- 1
- 上传者
#include <bits/stdc++.h>
using namespace std;
int n, x;
stack<pair<int, int>> s;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
long long ans = 0;
for (int i = 1; i <= n; i++)
{
cin >> x;
// 栈顶小于自己的
while (!s.empty() && s.top().first < x)
{
ans += s.top().second;
s.pop();
}
// 栈顶等于自己的
int num = 1; // 有几个 x
if (!s.empty() && s.top().first == x)
{
ans += s.top().second;
num += s.top().second;
s.pop();
}
// 栈顶大于自己的
if (!s.empty())
ans ++;
// 压入栈
s.push({x, num});
}
cout << ans;
return 0;
}