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 | #include <iostream> #include <stack> using namespace std; stack<long long> s; long long temp, res; int n; int main() { scanf(“%d”, &n); for (int i = 0; i < n; i++) { scanf(“%lld”, &temp); while (!s.empty() && s.top() <= temp) { s.pop(); } s.push(temp); res += s.size() – 1; } printf(“%lld\n”, res); return 0; } | cs |