fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. const int M = 1e9 + 7;
  5.  
  6. int main()
  7. {
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(NULL);
  10. int n;
  11. cin >> n;
  12. vector<int> v(n);
  13. for (int i = 0; i < n; i++)
  14. {
  15. cin >> v[i];
  16. }
  17. stack<int> st;
  18. int cnt = 0;
  19. for (int j = 0; j < n; j++)
  20. {
  21. if(j>0 && v[j]==v[j-1]) continue;
  22. while (!st.empty() && v[st.top()] <v[j])
  23. {
  24. st.pop();
  25. if(st.size()>0) cnt++;
  26. }
  27. st.push(j);
  28. }
  29. cout << cnt << endl;
  30. return 0;
  31. }
  32.  
  33. // 7
  34. // 6 3 1 1 2 4 5
Success #stdin #stdout 0.01s 5316KB
stdin
5
5 2 1 3 6
stdout
3