POJ 2559 Largest Rectangle in a Histogram(单调栈)
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 follown 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.
思路
因为每个矩形的宽都为1,高不等,要求拼接起来的矩形的面积的最大值,可以看做给定一列数,定义子区间的值为区间长度乘以区间最小值,求区间值最大为多少。直接枚举肯定T,所以以每个值为区间最小值,向左向右扩展延伸区间,然后更新最大值,也就是单调栈的思想。如果当前元素大于栈顶元素,那么这个元素是不能向前伸展的;如果当前元素小于栈顶元素,这个时候就要把栈中的元素一个一个弹出来,直到当前元素大于栈顶元素,对于弹出来的元素,它扩展到当前元素就不能向后伸展下去了,因此对于弹出来的元素这个时候就可以计算左右端点形成区间与最小值的乘积了,维护一个最大值就好了。
#include<stdio.h> #include<string.h> typedef __int64 LL; const int maxn = 100005; LL a[maxn],stack[maxn],left[maxn]; int main() { int N; while (~scanf("%d",&N) && N) { LL res = 0,tmp; memset(stack,0,sizeof(stack)); memset(left,0,sizeof(left)); for (int i = 1;i <= N;i++) scanf("%I64d",&a[i]); a[++N] = -1; //手动加上“-1”,使得所有元素都能入栈出栈 int top = 0; for (int i = 1;i <= N;i++) { if (!top || a[i] > a[stack[top-1]]) { stack[top++] = i; left[i] = i; continue; } if (a[i] == a[stack[top-1]]) continue; while (top > 0 && a[i] < a[stack[top-1]]) { top--; tmp = a[stack[top]]*((i-1)- (left[stack[top]]-1)); res = res<tmp?tmp:res; } tmp = stack[top]; stack[top++] = i; left[i] = left[tmp]; } printf("%I64d\n",res); } return 0; }
POJ 2559 Largest Rectangle in a Histogram(单调栈)的更多相关文章
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- PKU 2559 Largest Rectangle in a Histogram(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- POJ2559 Largest Rectangle in a Histogram —— 单调栈
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Lim ...
- 题解报告: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 ...
随机推荐
- 简单谈谈dom解析xml和html
前言 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.html,xml都是基于这个模型构造的.这也是一个W3C推出的标准.j ...
- Data URI 应用场景小结
Data URI scheme 在前端开发中是个常用的技术,通常会在 CSS 设置背景图中用到.比如在 Google 的首页就有用到: Data URI scheme 简称 Data URI,经常会被 ...
- Linux进程间通信之信号量
春节过去了,真的过去一年了.在公司待了快一年了.2016希望自己变得越来越好. ps:上面那句话是年前写的,中间隔了那么久,自己也变懒了. 一.信号量 1,信号量本质是一个计数器,控制访问共享资源的最 ...
- 使用docker发布spring cloud应用
本文涉及到的项目: cloud-simple-docker:一个简单的spring boot应用 Docker是一种虚拟机技术,准确的说是在linux虚拟机技术LXC基础上又封装了一层,可以看成是基于 ...
- 转载:ZooKeeper Programmer's Guide(中文翻译)
本文是为想要创建使用ZooKeeper协调服务优势的分布式应用的开发者准备的.本文包含理论信息和实践信息. 本指南的前四节对各种ZooKeeper概念进行较高层次的讨论.这些概念对于理解ZooKeep ...
- Log4net使用(一)
LogHelper.cs using NLog; using NLog.Targets; namespace MyProject.Tool.Log { public class LogHelper { ...
- C#之发送邮件汇总
最近想搞个网站,其中找回密码用到了我们常见到的利用邮箱找回.利用邮箱的好处是可以有效确认修改密码者的身份. 百度了几篇博客,各有千秋.最终采用了QI Fei同志的博客,有Demo下载,看了看思路清晰, ...
- php URL判断
// 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠) if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a ...
- 屠龙之路_坚持就是胜利_NinthDay
狭小的空间里,屠龙天团的少年们和alpha恶龙苦苦对峙了一夜!这恶龙还挺能熬的,厉害了word龙.无奈之下,五更天的时候,我们的屠龙少年只能先退出战场养精蓄锐,为了不让恶龙再次逍遥法外,机智的屠龙队长 ...
- redis的redis.conf文件详解
常用的: GENERAL: daemonize yes 守护进程 port 6379 指定Redis监听端口 requirepass 1 设置认证密码为1 REPLICATION: slave ...