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

Solution

方法一:记录左、右第一个比每个元素低的元素

1.从左往右做单增栈,弹栈时r[stack[top--]]=当前元素地址,即各个元素右侧第一个比其低的元素地址;最后清栈时站内元素的r为n+1;

2.从右往左做单增栈,弹栈时l[stack[top--]]=当前元素地址,即各个元素左侧第一个比其低的元素地址;最后清栈时站内元素的l为0;

3.对于每个元素计算其对应的最大面积max[i]=h[i]*(r[i]-l[i]),对所有max[i]取最大值即可;

鉴于本方法便于自己实现,再此不给出对应代码;

方法二:从左往右或从右往左进行一次单增栈,每次弹栈时更新最大面积

1.栈内每个单位存入两个元素:该单位高度height和对应可控宽度length,对于每个大于栈顶直接入栈的元素,stack[i].length=1;

2.对于需要先弹栈再入栈的元素,其length=弹栈所有元素length之和+1,因为被弹栈的元素的高度均≥当前元素,所以其可控范围应加上被其弹栈元素的length;

3.在弹栈过程中,记录一个temp为本次弹栈到当前为止弹出的宽度,因为为单增栈,所以每个高度均可控其后被弹栈元素的宽度,所以其对应的面积为s=temp*h[i],取max更新ans即可;

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. using namespace std;
  7. struct node{
  8. long long height,length;
  9. }stack[100100];
  10. long long n,m,i,j,k,h[100100];
  11. inline long long read(){
  12. long long x=0;
  13. bool f=true;
  14. char c;
  15. c=getchar();
  16. while(c<'0'||c>'9'){
  17. if(c=='-') f=false;
  18. c=getchar();
  19. }
  20. while(c>='0'&&c<='9'){
  21. x=(x<<1)+(x<<3)+(c^48);
  22. c=getchar();
  23. }
  24. return f?x:-x;
  25. }
  26. void calc(){
  27. long long top=1,maxs=0,temp=0;
  28. for(i=1;i<=n;++i) h[i]=read();
  29. stack[1].height=h[1];
  30. stack[1].length=1;
  31. for(i=2;i<=n;++i){
  32. temp=0;
  33. while(stack[top].height>=h[i]&&top>0){
  34. temp+=stack[top].length;
  35. maxs=max(maxs,stack[top--].height*temp);
  36. }
  37. stack[++top].height=h[i];
  38. stack[top].length=temp+1;
  39. }
  40. temp=0;
  41. while(top>0){
  42. temp+=stack[top].length;
  43. maxs=max(maxs,stack[top--].height*temp);
  44. }
  45. printf("%lld\n",maxs);
  46. return;
  47. }
  48. int main(){
  49. for(;;){
  50. n=read();
  51. if(!n)return 0;
  52. calc();
  53. }
  54. return 0;
  55. }

单调栈基础知识部分可以参考我的题解:http://www.cnblogs.com/COLIN-LIGHTNING/p/8474668.html

[POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)的更多相关文章

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

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

  2. 题解报告: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 ...

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

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

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

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

  5. poj 2559 Largest Rectangle in a Histogram 栈

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

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

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

  7. POJ 2559 Largest Rectangle in a Histogram -- 动态规划

    题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...

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

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

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

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

随机推荐

  1. MySQL 基于xtrabackup备份—热备工具

    xtrabackup(仅对InnoDB存储引擎支持热备) percona公司开发 改进的MySQL分支:percona-server 存储引擎改进:InnoDB —> XtraDB 使用本地的R ...

  2. laravel连接多个不同数据库的单例类

    在連接多個不同數據庫時,需要寫多個連接,爲了簡化該操作,可以使用該基類,不同的數據庫只要建立好相對應的類繼承該類,就可以使用ORM模型進行操作了. class singletonInstance { ...

  3. Snapseed玩出新高度,分分钟让你成p图大神! 转

    (,,・∀・)ノ゛嗨呀 小阔爱们! 不知道大家记不记得~ 上周我们的副条发了一篇: <看过他的照片,我才知道什么是创意摄影> 德国仅22岁超现实主义艺术家Justin Peters 创造了 ...

  4. BZOJ 1965 洗牌(扩展欧几里得)

    容易发现,对于牌堆里第x张牌,在一次洗牌后会变成2*x%(n+1)的位置. 于是问题就变成了求x*2^m%(n+1)=L,x在[1,n]范围内的解. 显然可以用扩展欧几里得求出. # include ...

  5. NoSQL - Redis应用场景

         问题的引入 DB(Oracle.MySQL.Postgresql等)+Memcached 这种架构模式在我们生产环境中十分常见,一般我们通过Memcached将热点数据加载到cache,应用 ...

  6. CGLib动态代理引起的空指针异常

    一个同事将公司的开发框架基于最新的Spring.Tomcat.Java版本作了部分修改,拿来开发运行之后,发现一个奇怪的空指针异常. 还原一下当时的场景,代码大概如下,所有的Servlet继承自Bas ...

  7. 2个 List<T>进行数据合并

    var userF = new List<User>(); User m1 = new User() { Id = "0" }; userF.Add(m1); var ...

  8. Active Directory中获取域管理员权限的攻击方法

    Active Directory中获取域管理员权限的攻击方法         译:by  backlion 0x00 前言 攻击者可以通过多种方式在Active Directory中获得域管理员权限, ...

  9. Linux内核分析3

    周子轩创作品转载请注明出处  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 gdb跟踪start_ke ...

  10. 【读书笔记】《HTTP权威指南》:Web Robots

    一.概述 Web机器人(Web Robots)是一种Web客户端的软件程序,它自动发起一系列的Web事务,从而在没有人工参与的状态下完成各种Web数据的搜集和处理. 典型的Web机器人有: 股票绘图机 ...