Largest Rectangle in a Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15831   Accepted: 5121

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 nintegers 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.

Source

这个题目看第一眼想到用dp来做,但是刚开始dp了一阵,没有啥思路,后来多设置两个数组就行了,还有学长给说了一种比较好的方法,用单调栈来做,其实就是优化dp。
dp的思路就是第 i 个柱子,它能形成的最大面积就是以它为高,尽量向两边扩展为宽的的面积,关键就是怎么往两边扩展,这时候就可以想到只要左边的柱子高度大于它的高度就行,所以向左找到第一个小于它的高度,同样向右找到第一个小于他的高度,左右所有的加起来乘以它的高度就是它的面积。所以遍历所有的柱子就可以了。问题就是如果一个一个的向左或者向右遍历时间复杂度是O(n2), 所以这时候用到dp,就是如果它比左边的柱子要小的话,直接跳到左边柱子的left边界位置,然后,继续比较是否小于边界,如果小于继续迭代。反之就是答案。具体代码如下:
代码一(dp):
/*************************************************************************
> File Name: poj_2559.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年04月08日 星期三 09时00分34秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#define INF 99999999999999
using namespace std;
typedef long long LL;
const int N = ;
LL h[N], l[N], r[N];
LL Max(LL a, LL b)
{
return a > b ? a : b;
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int n;
while (~scanf("%d", &n) && n)
{
memset(l, , sizeof(l));
memset(r, , sizeof(r));
for (int i = ; i <= n; i++)
scanf("%lld", &h[i]);
l[] = ; h[] = -;
r[n] = n + ; h[n + ] = -;
for (int i = ; i <= n; i++)//找左边元素
{
if (h[i] < h[i - ])
{
int tmp = l[i - ];
while (h[i] <= h[tmp])//动态规划方法找,如果不用这中方法,普通的tmp--找的话会超时
tmp = l[tmp];
l[i] = tmp;
}
else if (h[i] == h[i - ])
l[i] = l[i - ];
else
l[i] = i - ;
}
for (int i = n - ; i > ; i--)//找右边
{
if (h[i] < h[i + ])
{
int tmp = r[i + ];
while (h[i] <= h[tmp])
tmp = r[tmp];
r[i] = tmp;
}
else if (h[i] == h[i + ])
r[i] = r[i + ];
else
r[i] = i + ;
}
LL ans = -;
for (int i = ; i <= n; i++)
{
h[i] = (r[i] - l[i] - ) * h[i];
ans = Max(ans, h[i]);
}
printf("%lld\n", ans);
}
return ;
}

单调栈的思路是将这些柱子分别一个一个的判断,如果大于前面的那个那么前面比他大的就是0, 所以直接压栈,如果小于的话,弹栈,知道弹出小于它的为止,等于它,弹出来一个,压进去一个。

代码二(单调栈):

/*************************************************************************
> File Name: poj_2559_stack.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年04月08日 星期三 09时48分09秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;
typedef long long LL;
const int N = ;
LL h[N], r[N], l[N];
LL Max(LL a, LL b)
{
return a > b ? a : b;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n;
while (~scanf("%d", &n) && n)
{
memset(l, , sizeof(l));
memset(r, , sizeof(r));
for (int i = ; i <= n; i++)
scanf("%lld", &h[i]);
stack<int> S;//找出左边的元素比他大的或者等于它的个数,单调栈
S.push();//把额外的一个点压进去,防止栈弹空
h[] = h[n + ] = -;
for (int i = ; i <= n; i++)
{
if (h[i] < h[i - ])//如果后者比前者小
{
int cnt = ;
while (h[S.top()] >= h[i])
{
l[i] += l[S.top()] + ;
S.pop();
}
S.push(i);
}
else if (h[i] == h[i - ])
{
S.pop();
S.push(i);
l[i] = l[i - ] + ;
}
else
{
l[i] = ;
S.push(i);
}
}
stack<int> S2;//找处右边大于等于它的个数
S2.push(n + );
for (int i = n; i > ; i--)
{
if (h[i] < h[i + ])
{
int cnt = ;
while (h[S2.top()] >= h[i])
{
r[i] += r[S2.top()] + ;
S2.pop();
}
S2.push(i);
}
else if (h[i] == h[i + ])
{
S2.pop();
S2.push(i);
r[i] = r[i + ] + ;
}
else
{
S2.push(i);
r[i] = ;
}
}
LL ans = -;
for (int i = ; i <= n; i++)
{
h[i] *= (l[i] + r[i] + );
ans = Max(ans, h[i]);
}
printf("%lld\n", ans); }
return ;
}

过了一段时间又做了一遍。感觉代码比上面两个要好一点

代码三(单调栈):

/*************************************************************************
> File Name: largest.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年09月10日 星期四 18时54分01秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn = ;
int a[maxn];
int L[maxn], R[maxn];
int stack[maxn];//单调递增栈
int main()
{
int n;
while (~scanf("%d", &n) && n)
{
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
a[] = a[n + ] = -;//添加两个端点位置
int top = ;
stack[++top] = ;
for (int i = ; i <= n; i++)//求出左边做远能扩展到的位置
{
if (a[i] > a[i - ])
{
L[i] = i;
stack[++top] = i;
}
else
{
while (a[stack[top]] >= a[i]) top--;
L[i] = L[stack[top + ]];
stack[++top] = i;
}
}
top = ;
stack[++top] = n + ;
for (int i = n; i >= ; i--)//右边
{
if (a[i] > a[i + ])
{
R[i] = i;
stack[++top] = i;
}
else
{
while (a[stack[top]] >= a[i]) top--;
R[i] = R[stack[top + ]];
stack[++top] = i;
}
}
long long ans = ;
for (int i = ; i <= n; i++)
ans = max(ans, (long long)a[i] * (R[i] - L[i] + ));
printf("%lld\n", ans);
}
return ;
}

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

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

  2. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  3. PKU 2559 Largest Rectangle in a Histogram(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...

  4. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  5. stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  6. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  7. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  8. POJ2559 Largest Rectangle in a Histogram —— 单调栈

    题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Lim ...

  9. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

随机推荐

  1. Tag Helpers 介绍

    Tag Helpers 介绍 原文:Introduction to Tag Helpers作者:Rick Anderson翻译:刘浩杨校对:高嵩(Jack) 什么是 Tag Helpers? Tag ...

  2. var a =a || {}

  3. 关于wtl的一个实验

    代码如下: #include <iostream> using namespace std; template<typename T> class Base { public: ...

  4. java的"=="与"equals"

    equals 方法是 java.lang.Object 类的方法. 有两种用法说明: (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变 ...

  5. Surprising Strings(map类)

    http://poj.org/problem?id=3096 题意容易理解,开始直接暴力,还是用map写下吧,熟练一下: #include<stdio.h> #include<str ...

  6. COJ 2135 Day10-例1

    Day10-例1 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 给定N个物品,价格分别为A1, A2…AN.设计一套面值互不 ...

  7. COJ 2105 submatrix

    submatrix 难度级别: A: 编程语言:不限:运行时间限制:2000ms: 运行空间限制:131072KB: 代码长度限制:102400B 试题描述   小A有一个N×M的矩阵,矩阵中1~N* ...

  8. 【转】推荐--《Android深入浅出》----不错

    原文网址:http://www.cnblogs.com/plokmju/p/Android_Book.html 承香墨影   推荐--<Android深入浅出> 基本信息 书名:Andro ...

  9. 【转】Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例

    原文网址:http://www.cnblogs.com/skywang12345/p/3308556.html 上一章,我们学习了Collection的架构.这一章开始,我们对Collection的具 ...

  10. Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算

    目录(?)[-] proc文件系统 proccpuinfo文件 procstat文件 procpidstat文件 procpidtasktidstat文件 系统中有关进程cpu使用率的常用命令 ps ...