两种解法。当中一种是用单调栈。

我想到的是第二种:最大的矩形,中间一定有个最矮的某个单位矩形。所以求出每一个包括矩形histogram[i]的最大矩形的面积。输出这些面积中最大那个就可以。

key:用两个数组记录histogram[i]左右两边第一个比它小的单位矩形的序号leftLowerId[i]和rightLowerId[i]。那么对于histogram[i],它自己的最大矩形面积就是(rightLowerId[i] - leftLowerId[i] - 1) *  histogram[i]。

这里找leftLowerId和rightLowerId的时候用DP加速。以rightLowerId为例,找到右边比histogram[i]矮的矩形,停止,遇到比histogram[i]高的矩形j,直接跳到比histogram[j]矮的矩形rightLowerId[j].

#include<iostream>
using namespace std; //the histogram stored from left to right
long histogram[100001];
int rightLowerId[100001];
int leftLowerId[100001]; //from right to left
void FindRightSideLowerRec(int n)
{
rightLowerId[n - 1] = n; // there is no rectangle on its right
for (int i = n - 2; i >= 0; i--){ int cid = i + 1;
while (histogram[cid] >= histogram[i] && cid < n){
cid = rightLowerId[cid]; // the key
} rightLowerId[i] = cid;
}
} //from left to right
void FindLeftSideLowerRec(int n)
{
leftLowerId[0] = -1; // there is no rectangle on its left
for (int i = 1; i < n; i++){ int cid = i - 1;
while (histogram[cid] >= histogram[i] && cid > -1){
cid = leftLowerId[cid]; // the key
} leftLowerId[i] = cid;
}
} long long CalLargestRectangle(int n)
{
long long largestArea = 0; for (int i = 0; i < n; i++)
{
long long width = rightLowerId[i] - leftLowerId[i] - 1;
long long height = histogram[i]; long long area = width * height; if (area > largestArea)
largestArea = area;
} return largestArea;
} int main()
{
int n;
while (scanf("%d", &n)){
if (n == 0)
return 0; for (int i = 0; i < n; i++)
{
scanf("%d", &histogram[i]);
} FindRightSideLowerRec(n);
FindLeftSideLowerRec(n);
long long larea = CalLargestRectangle(n); printf("%I64d\n", larea); }
}

poj 2559求柱形图中最大矩形的更多相关文章

  1. poj 2349 求MST中第S大的权值

    题目大意: 有一些炮台,如果这个炮台有卫星接收器,那么任意两个有卫星接收器的炮台可以通信,不受距离限制:否者,两个炮台之间只能通过对讲机通信,这是受距离限制的.要买一种对讲机,用在需要的炮台上,要求所 ...

  2. poj 1961 (求字符串中的重复子串)

    Sample Input 3aaa12aabaabaabaab0Sample Output Test case #12 23 3 Test case #22 2 //aa有2个a6 2 //aabaa ...

  3. poj 2485 求最小生成树中 最长的一条边

    Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim # include <iostr ...

  4. poj 2406 求字符串中重复子串的个数

    Sample Input abcdaaaaababab.Sample Output 1 //1个abcd4 //4个a3 //3个ab #include<stdio.h> #include ...

  5. Maximal Rectangle, 求矩阵中最大矩形,参考上一题

    问题描述: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  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 栈

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

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

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

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

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

随机推荐

  1. 数据库学习:for xml path

    一.开发环境 数据库:SQLServer2012 二.语法简介 for xml path它以xml形式展示查询的结果集 三.语法介绍 现在数据库中有一张表 1.基本语法 select * from B ...

  2. WordPress熊掌号页面改造,自动发布

    写在前面的话: 有很多小伙伴刚进入WordPress,对很多东西还不太了解,比如:有的主题很挑剔,对于有些插件不兼容,但是呢对于这个功能有不可或缺.所以,这时候就需要我们自己手动修改或者添加代码,来实 ...

  3. Java运行报错问题——Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook

    http://blog.csdn.net/xifeijian/article/details/8830933 上述这个朋友博文提醒,可能是因为其他软件添加了JAVA_HOME的路径造成冲突.但他支持删 ...

  4. Tornado引入静态css、js文件

    一.静态路径 template_path=os.path.join(os.path.dirname(__file__), "templates") 这里是设置了模板的路径,放置模板 ...

  5. Java反射机制实战——字段篇

    首先,我们来认识几个类. Class(java.lang.Class) Class对象是一个特殊对象,每一个类都有一个Class对象,用来创建该类的“常规”对象.可以通过对象的getClass()方法 ...

  6. text-shadow的用法详解

    1.兼容性:text-shadow 和 box-shadow 这两个属性在主流现代浏览器上得到了很好的支持( > Chrome 4.0, > Firefox 3.5, > Safar ...

  7. 关于使用Axis2 webservice 处理Fault响应时抛org.apache.axis2.AxisFault的分析

    使用Axis2这个框架进行webservice协议通讯,期间出了个问题,我(CLIENT)请求后,当服务端返回符合协议的SOAP异常报文,例如<soap:fault> ... 我的程序直接 ...

  8. sp_Msforeachtable与sp_Msforeachdb详解

      一.简要介绍: 系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程.从mssql6.5开始,存放在SQL Server的MASTER数据 ...

  9. swift Hashable Equatable

    /// You can use any type that conforms to the `Hashable` protocol in a set or /// as a dictionary ke ...

  10. MySQL单表数据不超过500万:是经验数值,还是黄金铁律?

    今天,探讨一个有趣的话题:MySQL 单表数据达到多少时才需要考虑分库分表?有人说 2000 万行,也有人说 500 万行.那么,你觉得这个数值多少才合适呢? 曾经在中国互联网技术圈广为流传着这么一个 ...