fork download
  1. /*Code by HsonW, 11/2 NH-Hue. Just a newbie <3*/
  2. /*tai vi anh yeu duoi*/
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. using int64=long long;
  6. #define ll long long
  7. const int MOD=1e9+7;
  8. const int MAX=1e6+100;
  9. typedef pair<int,int> ii;
  10. typedef pair<ll,int> li;
  11. typedef pair<int,ll> il;
  12. typedef pair<ll,ll> LL;
  13. int n;
  14. signed main(){
  15. #define name "HsonW"
  16. ios::sync_with_stdio(0);
  17. cin.tie(NULL);
  18. if(fopen(name ".inp", "r")){
  19. freopen(name ".inp", "r", stdin);
  20. freopen(name ".out", "w", stdout);
  21. }
  22. stack<il>st;
  23. cin >>n;
  24. int ans=0;
  25. for(int i=1;i<=n;i++){
  26. int cnt=1;
  27. int a;
  28. cin >>a;
  29. while(!st.empty() && st.top().first<=a){
  30. ans+=st.top().second;
  31. if(st.top().first==a)cnt+=st.top().second;
  32. st.pop();
  33. }
  34. if(!st.empty()) ans++;
  35. st.push({a,cnt});
  36. }
  37. cout <<ans;
  38.  
  39. }
Success #stdin #stdout 0s 5320KB
stdin
7 
2 
4
1
2
2
5
1
stdout
10