Monotonous stack: maximum rectangle c ++.

The maximum rectangle

Here Insert Picture Description
Comprising a plurality of sets of input data. Each set of data is represented by an integer number n of small rectangular histogram, it can be assumed 1 <= n <= 100000. then the next n integers h1, ..., hn, satisfying 0 <= hi <= 1000000000. These figures left to right in the histogram represents the height of each of the small rectangle, the width of each small rectangle is 1. Test data to 0 at the end. For each line the test data output represents an integer answer.

sample input:
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

sample output:
8
4000

Ideas:

  • The final apparent width × height = rectangle size, a rectangle height is high, the width of rectangle is left to encounter a number of rectangular shorter than the first rectangle passes his
  • According to this line of thought we can actually use violence against each rectangle to have him around to look younger than him rectangle, and then calculate the width of the pass, but due to the large number of data given may burst
  • On both sides were looking for more trouble, if they can first rectangle arranged from low to high, then only need to calculate the number of his right rectangle, you can calculate the current rectangular area. (Can not be calculated because of the low width of the rectangle, you can become so unilateral from bilateral)
  • So that you can use monotonous stack of thought, the establishment of a stack, the stack is stored within the rectangle increasing height. In order to reach the destination, through each histogram rectangle: If the current height of the rectangle is higher than the top of the stack height of the rectangle, then it will push the current height; otherwise pop stack.
  • When the stack is calculated accumulated value width, the size of a rectangle calculated and compared the answers, whichever is greater update answers; then integrated into the current height and width, for the push.
  • After traversing the entire resultant ans rectangular area is the greatest, the time complexity of monotone stack only O (n)
  • The first type uses a stack done directly, then the TLE, it is described an array, int add a variable stack top corresponds. Note that the data scope of this question, ans stack and requires the use of long long, you need to compare the variables into a unified data types when comparing the max, you can cast
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
int main()
{
 int n;
 while(scanf("%d",&n)!=EOF&&n!=0)
 {
 int a[100011],w[100011];
 long long stack[100011];
 long long top=0;
 long long ans=0;
 memset(a,0,sizeof(a));
 for(int i=0;i<n;i++) scanf("%d",&a[i]);
 for(int i=0;i<=n;i++)
 {
  if(a[i]>stack[top])
  {
   stack[++top]=a[i];
   w[top]=1; 
  }
  else
  {
   int width=0;
   while(stack[top]>a[i])
   {
    width+=w[top];
    ans=max(ans,(long long)width*stack[top]);
    top--;
   }
   top++;
   stack[top]=a[i];
   w[top]=width+1;
  }
 }
 printf("%lld\n",ans);
 } 
 return 0;
 } 
Published 29 original articles · won praise 1 · views 941

Guess you like

Origin blog.csdn.net/qq_44654498/article/details/104973739