POJ2559 largest Rectangle in a histogram

Source: Internet
Author: User

largest Rectangle in a histogram
time Limit:  1000ms   Memory Limit:  65536k
total Submissions:  26987   Accepted:  8727

Description

a histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles has equal widths but could have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the Heights 2, 1, 4, 5, 1, 3, 3, Measured in units where 1 is the width of the rectangles:

Usually, histograms is used to represent discrete distributions, e.g., the frequencies of characters in texts. Note the order of the rectangles, i.e, their heights, is important. Calculate the area of the largest rectangle in a histogram that's aligned at the common base line, too. The shows the largest aligned rectangle for the depicted histogram.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is compos Ed of. Assume that 1<=n<=100000. Then follow n integers H1,..., hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles in histogram order. The width of each rectangle is 1. A Zero follows the input for the last Test case.

Output

The For all test case is output on a single line, the area of the largest rectangle in the specified histogram. Remember that this rectangle must is aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 34 1000 1000 1000 10000

Sample Output

84000

Hint

Huge input, scanf is recommended.

Source

ULM Local 2003 * * * * This problem is a monotonous stack of Stact<int\float\double> St is to build a stack, ST is the name of this stack, St.push (i) to put I into this stack inside, st.top () stack top elements, St.pop () deletes the top element of the stack, St.empty () determines whether the stack is empty, and st.size () returns the size of the stack. complexity O (n)Application:

1. The most basic application is given a set of numbers, for each number, look for it and the number of the first on its right is larger than it.

2. Given a sequence, look for a subsequence so that the smallest value in the subsequence is multiplied by the length of the subsequence.

3. Given a sequence, look for a subsequence so that the smallest value in the subsequence is multiplied by all the elements of the subsequence and the largest.

 
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5#include <stack>6 using namespacestd;7 intMain ()8 {9     intI,n,top;//Top pointing to the top of the stackTenstack<int> st;//the stack is used to hold the number of the rectangle, i.e. the position One     Long Long inttmp,ans,a[100010];//tmp is a temporary variable, the value of the area is recorded, ans is the result, and the maximum area value is recorded A      while(SCANF ("%d", &n) &&N) -     { -          for(i =1; I <= n;i++) the         { -scanf"%lld",&a[i]); -         } -Ans =0; +A[n +1] = -1;//The last element is set to the minimum value to finally empty the stack -          for(i =1; I <= n +1; i++) +         { A             if(St.empty () | | a[i] >=a[st.top ()]) at{//if the stack is empty or the stack element is greater than or equal to the top element of the stack - St.push (i); -             } -             Else -             { -                  while(!st.empty () && A[i] <a[st.top ()]) in{//if the stack is not empty and the stack element is less than the top element of the stack, the stack top element is stacked -top =st.top (); to St.pop (); +TMP = (i-top) * A[top];//calculating area values during the stack-out process -                     if(tmp > ANS)//Update Area maximum value the                     { *Ans =tmp; $                     }Panax Notoginseng                 } -St.push (top);//Update this value to the latest location theA[top] =A[i]; +             } A         } theprintf"%lld\n", ans); +      }  -      return 0; $}

POJ2559 largest Rectangle in a histogram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.