[Programming thinking and practice Week5 job A maximum rectangle]

Subject description:

To a histogram, find the largest rectangular area in the histogram. For example, the following image height in the histogram from left to right are 2, 1, 4, 5, 1, 3, 3, 1 they are wide, the largest rectangle is shaded.
Here Insert Picture Description

Input formats:

Comprising a plurality of sets of input data. Each set of data is represented by an integer number n of small rectangular histogram, you can assume that 1 <= n <= 100000. then the next n integers h1, ..., hn, satisfying 0 <= hi <= 1000000000. These histogram from left to right the digital representation of each small rectangle of height, the width of each small rectangle is 1. Test data to 0 at the end.

Sample Input:

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Output formats:

For each line the test data output represents an integer answer.

Sample Output:

8
4000

Ideas:

Requires taking the maximum area, the focus is the height of each histogram to the left of the maximum width of the rectangle rightward malleable.
Violent solution idea is to start from each small rectangle, traverse to the left, find the location of the first shorter than its small rectangles, calculate the total width, multiply the rectangle with the largest area of the current record high comparison taking the maximum. But no doubt this will result in a timeout. Therefore, we should use a method of obtaining lower time width of each rectangular extensible right and left.
Is used here monotone stack method, traverse respective rectangular sequentially press-fitting this press-fitting while maintaining monotonicity of the stack. For incrementing the stack (stack top to the stack bottom increments) to less than the current element is pressed into the top element, the top element of the pop-up, and can determine the position of the top element extending into the farthest right elements to be pushed -1 until the elements to be pushed to meet the stack will be pressed into the monotonicity. After traversing all the remotest position rectangles for the remaining elements in the stack, which extends right to the top element of doubt the location. After next iteration of its rightmost position will be determined.
Then traversed backward incremental respective rectangular stack, which may also be extended to determine the position of the left, the only difference is that the maximum position of the top element to be ejected may extend leftward position +1 to be pushed to the element, and non-1, because it is traversed backward.
After determining the width of the rectangle of each extension, it may be traversed in order to obtain the maximum area, so that time can be reduced to a linear complexity level.

Code:

#include <iostream>
using namespace std; 
const int size=1e5+10;
int n,a[size],L[size],R[size],st[size];
int main(int argc, char** argv) {
while(scanf("%d",&n)&&n!=0)
 {
  for(int i=1;i<=n;i++)
   scanf("%d",&a[i]);
  
  int l=1,r=0;
  for(int i=1;i<=n;i++)
  {
   while(l<=r&&a[st[r]]>a[i])
   {
    R[st[r]]=i-1;
    r--;
   }
   st[++r]=i;
  }
  while(l<=r)
  {
   R[st[l]]=st[r];
   l++;
  }
  for(int i=n;i>=1;i--)
  {
   while(l<=r&&a[st[r]]>a[i])
   {
    L[st[r]]=i+1;
    r--;
   }
   st[++r]=i;
  }
  while(l<=r)
  {
   L[st[l]]=st[r];
   l++;
  }
  long long ans=0;
  for(int i=1;i<=n;i++)
  {
   long long temp=(R[i]-L[i]+1)*(long long)a[i];
   ans=max(ans,temp);
  }
  
  printf("%lld\n",ans);
 }
 return 0;
}
Published 24 original articles · won praise 8 · views 529

Guess you like

Origin blog.csdn.net/weixin_44034698/article/details/104982495