poj 2559求柱形图中最大矩形
两种解法。当中一种是用单调栈。
我想到的是第二种:最大的矩形,中间一定有个最矮的某个单位矩形。所以求出每一个包括矩形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求柱形图中最大矩形的更多相关文章
- poj 2349 求MST中第S大的权值
题目大意: 有一些炮台,如果这个炮台有卫星接收器,那么任意两个有卫星接收器的炮台可以通信,不受距离限制:否者,两个炮台之间只能通过对讲机通信,这是受距离限制的.要买一种对讲机,用在需要的炮台上,要求所 ...
- poj 1961 (求字符串中的重复子串)
Sample Input 3aaa12aabaabaabaab0Sample Output Test case #12 23 3 Test case #22 2 //aa有2个a6 2 //aabaa ...
- poj 2485 求最小生成树中 最长的一条边
Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim # include <iostr ...
- poj 2406 求字符串中重复子串的个数
Sample Input abcdaaaaababab.Sample Output 1 //1个abcd4 //4个a3 //3个ab #include<stdio.h> #include ...
- Maximal Rectangle, 求矩阵中最大矩形,参考上一题
问题描述: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 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 ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- 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(单调栈) && 单调栈
嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...
随机推荐
- 解决Unicode编码(璘)
随着互联网发展,B/S越来越受欢迎 Code编码格式也越来载多, 在大千花花世界 中文在Web显示看似一样但实际编码并不样,导致从页面获取的资料录入到数据库中时 存取的就是Code编码 如:Unico ...
- Laravel5.1学习笔记21 EloquentORM 集合
Eloquent: Collections Introduction Available Methods Custom Collections Introduction All multi-resul ...
- C#.NET,技巧篇(DataGridView线程操作)
这个系列的文章,主要是平时做C#.NET(Framework 3.5)开发的时候,积累的经验和技巧.我们平时总有这样的体会,遇到一个特别难解决的问题,网上寻它千百度也没能搜索到有用的信息.这时你肯定会 ...
- 初始MongoDB------将MongoDB创建为Windows服务
上一遍我写的是关于基本的MongoDB的安装,可能不是很详细,也写得很不好,不过这次我们会详细的说说,如果将MongoDB部署在你的Windows电脑上. 1.配置环境变量 如果每次都要在CMD进入M ...
- 梦想CAD控件 2018.10.15更新
下载地址: http://www.mxdraw.com/ndetail_10105.html 1. 完善com接口的ToCurves函数,转换CAD文字,多行文字到曲线 2. 修改DrawImage接 ...
- nginx_location用法总结
location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...
- LeetCode15——3Sum
数组中找三个数和为0的结果集 1 // 解法一:先排序 然后固定一个值 然后用求两个数的和的方式 public static List<List<Integer>> three ...
- Manacher算法(马拉车算法)浅谈
什么是Manacher算法? 转载自百度百科 Manachar算法主要是处理字符串中关于回文串的问题的,它可以在 O(n) 的时间处理出以字符串中每一个字符为中心的回文串半径,由于将原字符串处理成两倍 ...
- 微信小程序开发过程中tabbar页面显示的相关问题及解决办法!
在微信小程序的开发过程中如果有使用过tabbar的同学,我相信一定会遇到一些困扰.为什么有些时候代码中明明已经在app.json里面增加了tabbar,可以页面中就是不显示呢?可不可以有些页面显示ta ...
- BZOJ 3916 [Baltic2014]friends
[题解] 暴力枚举添加字符的位置,然后判断去掉这个位置的字符串是否有由两个相同的串拼接而成.本题的n的范围为200W,暴力判断会TLE,所以要使用哈希. #include<cstdio> ...