POJ 2559 Largest Rectangle in a Histogram

原创 2016年08月29日 16:54:29

Description

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may 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 are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at 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 may assume that 1<=n<=100000. Then follow n integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the 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 output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be 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.


求矩形最大面积,单调栈的应用。

#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(i,j,k) for (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 first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using 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 * 10 + 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);
		}
		per(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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 1506 && POJ 2559 Largest Rectangle in a Histogram (单调队列)

题目链接:POJ 2559  Largest Rectangle in a Histogram  题目链接:HDU 1506  Largest Rectangle in a Histogram  题...

【POJ2559】Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

POJ_2559_Largest Rectangle in a Histogram(栈)

题意:柱状图是由一些宽度相等的长方形下端对齐后横向排列得到的图形。现在有由n个宽度为1,高度分别为h1,h2,……,hn的长方形从左到右依次排列组成的柱状图。问里面包含的长方形的最大面积是多少。...

POJ2559 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

[POJ 2559 Largest Rectangle in a Histogram] 单调栈

[POJ 2559 Largest Rectangle in a Histogram] 单调栈题目链接:[POJ 2559 Largest Rectangle in a Histogram] 题意...

poj2559 Largest Rectangle in a Histogram hdu1505 City Game

POJ2559 题意: 在直方图中找出最大矩形 如图,输入数据为 7 2 1 4 5 1 3 3 7为个数,直方图如上图 枚举所有宽为1的矩形,往两边扩充,找出完整包含...

Largest Rectangle in a Histogram-POJ2559

原题链接http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence...
  • doc_sgl
  • doc_sgl
  • 2016年10月04日 15:44
  • 262

POJ 2559 Largest Rectangle in a Histogram

1.题目描述:点击打开链接 2.解题思路:本题利用单调栈解决。单调栈是满足从栈顶到栈底,元素大小严格递增或者严格递减的栈。对于本题,如果我们可以知道以i为端点,向左可以最远延伸的距离和向右最远延伸的...

【POJ 2559 Largest Rectangle in a Histogram】+ 栈

Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2...

poj2559 & zoj1985 &hdu1506 Largest Rectangle in a Histogram(笛卡尔树)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 2559 Largest Rectangle in a Histogram
举报原因:
原因补充:

(最多只允许输入30个字)