Guest User

Untitled

a guest
Feb 4th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include<iostream>
  2. #include<stack>
  3. using namespace std;
  4.  
  5. int getMaxArea(long long hist[], int n)
  6. {
  7.     stack<int> s;
  8.     long long max_area = 0;  
  9.     int tp;
  10.     long long  area_with_top;
  11.                    
  12.     int i = 0;
  13.     while (i < n)
  14.     {
  15.         if (s.empty() || hist[s.top()] <= hist[i])
  16.             s.push(i++);
  17.  
  18.         else
  19.         {
  20.             tp = s.top();
  21.             s.pop();
  22.  
  23.             area_with_top = hist[tp] * (s.empty() ? i :
  24.                                 i - s.top() - 1);
  25.  
  26.             if (max_area < area_with_top)
  27.                 max_area = area_with_top;
  28.         }
  29.     }
  30.  
  31.     while (s.empty() == false)
  32.     {
  33.         tp = s.top();
  34.         s.pop();
  35.         area_with_top = hist[tp] * (s.empty() ? i :
  36.                                 i - s.top() - 1);
  37.  
  38.         if (max_area < area_with_top)
  39.             max_area = area_with_top;
  40.     }
  41.  
  42.     return max_area;
  43. }
  44.  
  45. int main()
  46. {   int n;
  47.     cin>>n;
  48.     long long hist[n] ;
  49.     for(int i = 0; i < n; i++)
  50.         cin>>hist[i];
  51.     cout << getMaxArea(hist, n);
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment