nicuvlad76

Untitled

Feb 18th, 2023
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #define N 100005
  4. using namespace std;
  5. ///2650
  6. ifstream fin(".in");
  7. ofstream fout(".out");
  8.  
  9. int st[N], dr[N], a[N],s[N];
  10. int n ,top,i,x;
  11. long long sum=0;
  12. int main()
  13. {
  14.   cin>>n;
  15.   for(i=1;i<=n;i++)cin>>a[i];
  16.   /// 2 santinele
  17.   a[0]=a[n+1]=N;
  18.   ///construim vector la stg
  19.   s[1]=0;
  20.   for(i=1;i<=n;++i)
  21.   {
  22.      x=a[i];
  23.      while(top>0 && a[s[top]]<x)top--;
  24.      st[i]=i-s[top];
  25.      s[++top]=i;
  26.   }
  27.   ///cosntruim vector la dr
  28.   s[1]=n+1;
  29.   top=1;
  30.   for(i=n;i>=1;--i)
  31.   {
  32.       x=a[i];
  33.       while(top>0 && a[s[top]]<x)top--;
  34.       dr[i]=s[top]-i;
  35.       s[++top]=i;
  36.   }
  37.   ///suma
  38.   for(int i=1;i<=n;i++)
  39.     sum+=a[i]*st[i]*dr[i];
  40.   cout<<sum;
  41.   return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment