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.
 
题意:n个矩形,然后给出每个矩形的高(宽度都为1),求出其中最大矩形的面积
 
思路:对于高度递增的矩形序列,我们可以尝试以每一块的高度为最终高度,然后向后延申宽度,最大面积就是答案。
但是当前高度小于之前矩形的高度时,我们可以先回溯,之前的矩形肯定时高度递增的,回溯的目的时更新之前矩形所能形成的最大的答案

(黄色区域为回溯时,矩形高度仍大于当前矩形,更新的答案)

 
直到之前的矩形高度小于当前矩形,就将之前所积累的(宽度+1),当成新矩形的宽度,高度就是当前矩形高度,这样的对于后面的矩形,又形成了新的递增型矩形序列
而且由于回溯的时候,我们将舍弃的部分能形成的最大面积已经考虑了,所以不会出现答案遗失(对于后面的矩形紫色无法利用的,被当前矩形限制了高度,所以舍弃,加入扩展了的当前矩形)

最后,对整个递增的矩形序列进行一次回溯,答案的更新,为了方便将其最后加入一个高度为0的矩形,当然不加另外判断也ok

(用不用栈无所谓,重要的是单调性)

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std; typedef long long ll;
const int maxn = 1e5+;
stack<ll>s;
ll ans;
int w[maxn];
int h[maxn];
int n;
int main()
{
while(~scanf("%d",&n) && n)
{
for(int i=;i<=n;i++)scanf("%d",&h[i]);
while(!s.empty())s.pop();
int pos = ;
h[n+] = ;
ans = ;
for(int i=;i<=n+;i++)
{
if(s.empty() || h[i] >= s.top())
{
s.push(h[i]);
w[++pos] = ;
}
else
{
int width = ;
while(!s.empty() && s.top() > h[i])
{
width += w[pos];
ans = max(ans,s.top()*width);
s.pop();
pos--;
}
s.push(h[i]);
w[++pos] = width+;
}
}
printf("%lld\n",ans);
}
}

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

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

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

  2. 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)

    题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...

  3. Largest Rectangle in a Histogram POJ - 2559

    很显然是单调栈 这里记录一种新的写法,这种写法基于递推,但是相比之下比单调栈更好写 #include<cstdio> #include<map> #include<set ...

  4. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

  5. poj 2559 单调栈 ***

    给出一系列的1*h的矩形,求矩形的最大面积. 如图: 题解链接:点我 #include <iostream> #include <cstdio> using namespace ...

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

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

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

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

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

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

  9. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

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

随机推荐

  1. C#简单画图程序

    实现过程: (1) 新建窗体应用程序 (2) 添加一个MenuScrip控件:添加一个ToolScrip控件. 在ToolScrip控件中对每个单元,要将DisplayStyle属性改为Text (3 ...

  2. oracle  的sqlplus 工具进行翻译的rlwrap 安装教程

    一:下载地址: 链接: https://share.weiyun.com/50R5pBb (密码:dQPc) 或者该QQ群下载: 二:该工具的安装步骤: [oracle@localhost ~]$ l ...

  3. Confluence 6 workbox 包含从 Jira 来的通知

    如果你的 Confluence 站点链接了一个 Jira 应用,你可以包含从 Jira 应用来的通知,例如 Jira 软化或 Jira 服务器桌面. 希望包含有从 Jira 应用来的通知: 你的 Ji ...

  4. django中数据库的配置及相关增删改查

    ORM ORM是什么?:(在django中,根据代码中的类自动生成数据库的表也叫--code first) ORM:Object Relational Mapping(关系对象映射) 类名对应---- ...

  5. Linux用户、用户组、文件权限学习笔记

    最近打算更仔细学习一下linux操作系统.先是恶补了一下用户.用户组.文件权限这三样比较重要的知识.学习这几样东西,得先掌握linux的权限系统相关知识.linux的权限系统主要是由用户.用户组和权限 ...

  6. LeetCode(70): 爬楼梯

    Easy! 题目描述: 假设你正在爬楼梯.需要 n 步你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 ...

  7. laravel 关联查询

  8. python网络爬虫笔记(三)

    一.切片和迭代 1.列表生成式 2.生成器的generate,但是generate保存的是算法,所以可以迭代计算,没有必要,每次调用generate 二.iteration 循环 1.凡是作用于for ...

  9. Vue-CLI 3.x 设置反向代理

    最近在项目中使用了Vue CLI 3.0版本,项目中需要设置反向代理解决跨域问题,下面记录一下设置过程. 新建配置文件 (vue-cli3.x 官网的配置文档 https://cli.vuejs.or ...

  10. MySQL源码安装一键脚本

    #红色部分根据自己的需求来定义#!/bin/bash #卸载系统自带的Mysql /bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps /bi ...