1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int N = 1e5+10;
int n; int a[N], b[N]; int sta[N];
int main() { cin >> n; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int head = 0; sta[++head] = n; b[n] = 0; for(int i = n-1; i >= 1; i--) { while(head && a[i] > a[sta[head]]) { b[i]++; b[i] += b[sta[head]]; head--; } sta[++head] = i; } long long sum = 0;
for(int i = 1; i <= n; i++) { sum += b[i]; } printf("%lld\n", sum);
return 0; }
|