POJ 2559 Largest Rectangle in a Histogram__poj

Source: Internet
Author: User

Description A histogram is a polygon composed of a sequence to rectangles at a aligned base line. The rectangles have equal widths but may have different. For example, the "figure on" the left shows the histogram, consists of rectangles with the Heights 2, 1, 4, 5, 1, 3, 3, Measured in units where 1 are the width of the rectangles: 

Usually, histograms are used to represent discre Te distributions, e.g., the frequencies of characters in texts. Note This order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram this is aligned on the common base line, too. The figure on the right 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 composed of. You are assume that 1<=n<=100000. Then follow n integers h1,..., hn, where 0<=hi<=1000000000. These numbers denote the heights's rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A Zero follows the input for the last test case.

Output for each test case is output on a single line 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 3 4
1000 1000 1000 1000
0

Sample Output

8
4000

Hint

Huge input, scanf is recommended.


The application of the maximum rectangular area and the monotonic stack is obtained.

#include <set> #include <map> #include <ctime> #include <cmath> #include <stack> #include <queue> #include <bitset> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> #include <functional> #define REP (i,j,k) for (int i = j; I <= K; i++) # Define per (int i = j; I >= K; i--) #define Loop (I,J,K) for (int i = J;i!=-1; i = k[i]) #define Lson x ;< 1, L, mid #define Rson x << 1 | 1, Mid + 1, r #define FI #define SE second #define MP (I,J) Make_pair (i,j) #define PII pair<string,string>
G namespace Std;
typedef long Long LL;
const int low (int x) {return x&-x;} const double EPS = 1e-8;
const int INF = 0x7fffffff;
const int mod = 1E8;
const int N = 3e5 + 10;
	const int Read () {char ch = getchar ();
	while (ch< ' 0 ' | | | ch> ' 9 ') ch = getchar ();
	int x = ch-' 0 '; while (ch = getchar ()) >= ' 0 ' &&ch <= ' 9 ') x = x * + ch-' 0 ';
return x;

int n, A[n], l[n], r[n];
		int main () {while (n = read (), N) {Rep (i, 1, N) scanf ("%d", &a[i]);
		Stack<int> p;
		A[0] = a[n + 1] =-1;
			Rep (i, 1, n + 1) {while (!p.empty () && A[i] < A[p.top ()]) r[p.top ()] = I-1, P.pop ();
		P.push (i);
			Each (i, n, 0) {while (!p.empty () && A[i] < A[p.top ()]) l[p.top ()] = i + 1, p.pop ();
		P.push (i);
		LL ans = 0;
		Rep (i, 1, n) ans = max (ans, 1LL * a[i] * (R[i]-l[i] + 1));
	printf ("%lld\n", ans);
return 0; }


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.