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

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

 
 
 
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<stack>
using namespace std; struct node
{
int h,id;
node(int a,int b){ h=a; id=b; } //h存高度,id存第几块
};
int n;
stack<node> Q;
long long a[],l[],r[];
//在a[i]的高度下,能达到的左边界l[i],能达到的右边界r[i] int main()
{
while(~scanf("%d",&n))
{
if(n==) break;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
while(!Q.empty()) Q.pop(); //清空栈Q
memset(l,,sizeof(l));
memset(r,,sizeof(r));
//求l[]数组,O(n)复杂度找左边界
for(int i=;i<=n;i++)
{
if (Q.empty()) {l[i]=; Q.push(node(a[i],i)); continue; }
node u=Q.top();
if (u.h<a[i]) {l[i]=i; Q.push(node(a[i],i)); continue;}
while(u.h>=a[i]) //如果当前高度a[i],比栈顶元素低,则栈顶元素能到达的左边界也能到达。
{
l[i]=l[u.id];
Q.pop();
if (Q.empty()) break;
u=Q.top();
}
Q.push(node(a[i],i) );
} //求r[]数组,扫右边界
while(!Q.empty()) Q.pop();
for(int i=n;i>=;i--)
{
if (Q.empty()) {r[i]=i; Q.push(node(a[i],i)); continue; }
node u=Q.top();
if (u.h<a[i]) {r[i]=i; Q.push(node(a[i],i));continue;}
while(u.h>=a[i])
{
r[i]=r[u.id];
Q.pop();
if (Q.empty()) break;
u=Q.top();
}
Q.push(node(a[i],i) );
} long long sum=;
for(int i=;i<=n;i++)
sum=max(sum,a[i]*(r[i]-l[i]+));
printf("%lld\n",sum);
}
return ;
}

ZOJ 1985 Largest Rectangle in a Histogram(刷广告)2010辽宁省赛的更多相关文章

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

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

  2. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

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

  3. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

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

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

  5. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  6. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

  7. Largest Rectangle in a Histogram

    2107: Largest Rectangle in a Histogram Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 777  Solved: 22 ...

  8. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  9. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. 【NOI2013】树的计数

    Description 我们知道一棵有根树可以进行深度优先遍历(DFS)以及广度优先遍历(BFS)来生成这棵树的DFS序以及BFS序.两棵不同的树的DFS序有可能相同,并且它们的BFS序也有可能相同, ...

  2. 将正在使用的Ubuntu14.04 制作成镜像文件

    remastersys 是一个能够备份你的ubuntu系统的工具. 源码在github上能找到:Remastersys Source 可以直接 apt 安装: sudo add-apt-reposit ...

  3. 554C - Kyoya and Colored Balls

    554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...

  4. mapStateToProps,mapDispatchToProps的使用姿势

    本文作者:IMWeb 黄qiong 原文出处:IMWeb社区 未经同意,禁止转载 前言 刚接触redux的时候,发现大家对mapDispatchToProps使用有几种方法,而且都跑通了,本文来介绍下 ...

  5. angular5中使用echart的方法

    注意两点安装的版本 安装好后可以参照echart的官网使用 1.实现package.json中安装这两个包 2.index.html中引入 3.在appModule中添加 然后再html中就可以这么使 ...

  6. Redis分布式锁实现简单秒杀功能

    这版秒杀只是解决瞬间访问过高服务器压力过大,请求速度变慢,大大消耗服务器性能的问题. 主要就是在高并发秒杀的场景下,很多人访问时并没有拿到锁,所以直接跳过了.这样就处理了多线程并发问题的同时也保证了服 ...

  7. 为用户管理连接 Confluence 6 到 Jira 应用程序

    请注意,在使用这个功能的时候,你的 Jira 应用许可证数量和 Confluence 的许可证数量不需要完全等同.例如,你可以通过 Jira 管理一个 50 个用户的 Confluence 许可证,尽 ...

  8. 字符串练习——唐纳德与子串 (Easy)

    G1. 唐纳德与子串 (Easy) Time limit per test: 1.0 seconds Memory limit: 256 megabytes 子串的定义是在一个字符串中连续出现的一段字 ...

  9. 记一次使用utl_http方法调用接口,报字符或值错误

    背景:ebs系统和其他系统通过utl_http包调用接口,使用log方法记录日志. 某次调用接口,执行到记录日志行报字符或值错误. 查找原因,发现是p_str的长度超过的32767的限制. 解决办法: ...

  10. 在Windows下配置svn服务端钩子程序(部分)

    需求一,svn提交时必须填写log日志的需求,如何进行配置呢?请看下面. 需要在版本库目录下找到hooks文件夹,我的版本库是dxoffice,所以是这个目录,你要找自己的目录 然后进入,创建一个pr ...