Advertisement
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[], long long n){
- stack<int> s;
- int max_area = 0;
- int tp;
- int 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() {
- long long n;
- cin>>n;
- long long m;
- cin>>m;
- long long w[m];
- long long hist[m];
- for(int i=0;i<m;i++){
- cin>>w[i];
- hist[i] = n - w[i];
- }
- cout<<getMaxArea(hist, m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement