Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stack>
- using namespace std;
- int getMaxArea(long long hist[], int n)
- {
- stack<int> s;
- long long max_area = 0;
- int tp;
- long long area_with_top;
- int i = 0;
- while (i < n)
- {
- if (s.empty() || hist[s.top()] <= hist[i])
- s.push(i++);
- else
- {
- tp = s.top();
- s.pop();
- area_with_top = hist[tp] * (s.empty() ? i :
- i - s.top() - 1);
- if (max_area < area_with_top)
- max_area = area_with_top;
- }
- }
- while (s.empty() == false)
- {
- tp = s.top();
- s.pop();
- area_with_top = hist[tp] * (s.empty() ? i :
- i - s.top() - 1);
- if (max_area < area_with_top)
- max_area = area_with_top;
- }
- return max_area;
- }
- int main()
- { int n;
- cin>>n;
- long long hist[n] ;
- for(int i = 0; i < n; i++)
- cin>>hist[i];
- cout << getMaxArea(hist, n);
- return 0;
- }
Add Comment
Please, Sign In to add comment