You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
955 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 80010;
stack<int> stk;
LL a[N], res[N];
int n;
LL ans;
/**
6
10 3 7 4 12 2
10 3 7 4 12 2
1 2 3 4 5 6
12 7 12 12 -1 -1
5 3 5 5 -1 -1
*/
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++) {
while (!stk.empty() && a[stk.top()] <= a[i]) {
res[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
for (int i = 1; i <= n; i++)
if (res[i]) ans += res[i] - i - 1;//两个号之间夹的需要j-i-1,比如4和2之间其实就是一个3就是4-2-1=1
else ans += n - i; //如果右侧没有比自己大的,那么就是一直到最右端,它都能看到。
//输出结果
cout << ans << endl;
return 0;
}