The largest rectangle Problem description There are n adjacent rectangles on the horizontal axis. The width of each rectangle is 1, and the height of the i-th (1 ≤ i ≤ n) rectangle is hi. These n rectangles form a histogram. For example, the heights of the six rectangles in the figure below are 3, 1, 6, 5, 2, 3. Please find the rectangle with the largest area that can be placed in the given histogram, and its sides should be parallel to the coordinate axis. For the example given above, the largest rectangle is the shaded part shown in the figure below, and the area is 10. Input format The first line contains an integer n, the number of rectangles (1 ≤ n ≤ 1000). The second line contains n integers h1, h2,…, hn, and adjacent numbers are separated by spaces. (1 ≤ hi ≤ 10000). hi is the height of the i-th rectangle. Output format Output a line containing an integer, that is, the area of the largest rectangle in the given histogram. Sample input 6 3 1 6 5 2 3 Sample output 10
#include<bits/stdc++.h>
using namespace std;intmain(){
int n, a[1010], area, temp, ans =0;
cin>>n;for(int i =0; i < n; i++)
cin>>a[i];
a[n]=0;
stack<int> s;for(int i =0; i <= n; i++)if(s.empty()|| a[s.top()]< a[i])
s.push(i);else{
temp = s.top();
s.pop();
area = a[temp]*(s.empty()? i : i - s.top()-1);if(area > ans)
ans = area;
i--;}
cout<<ans<<endl;return0;}