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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}