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.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...
随机推荐
- reduce的特殊用法
//计算数组中每个元素出现的次数var arr = ["apple","orange","apple","orange" ...
- python语言真正的奥义所在--对接32单片机
2018-02-2720:51:24 今天晚上注定我要玩一夜这个东西,太爽了,给力! 烧写固件成功, http://blog.csdn.net/Lingdongtianxia/article/deta ...
- Python 语言搭建SELENIUM测试环境,搭建过程记录。
第一步,安装Python: 第二步,安装SetupTools: 第三步,安装Pip: 第四步,安装selenium(for python) 第五步,新建第一个基于Firefox的测试用例 上述 只是步 ...
- MAMP中Python安装MySQLdb
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Py ...
- "HybridDB · 性能优化 · Count Distinct的几种实现方式” 读后感
原文地址:HybridDB · 性能优化 · Count Distinct的几种实现方式 HybridDB是阿里基于GreenPlum开发的一款MPP分析性数据库,而GreenPlum本身基于Post ...
- Farseer.net轻量级开源框架 中级篇:数据库切换
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 动态数据库访问 下一篇:Farseer.net轻量级开源框架 中级篇: SQL执行 ...
- glic,uClibc,EGLIBC 简要介绍
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- struts2特殊符号替换
今天用struts2做了一个小例子,结果发现个问题 action代码如下 private String table; public String execute(){ setName("pe ...
- ThinkPHP---thinkphp控制器、路由、分组设置(C)
配置文件分3类:系统配置文件,分组配置文件,应用配置文件 ①系统配置文件ThinkPHP/Conf/convention.php: ②分组 / 模块 /平台配置文件Home/Conf/config.p ...
- 03Struts2基本使用流程
Struts2基本使用流程 1.新建web工程 2.引入struts2类库 3.创建并配置Struts2的核心控制器web.xml用来拦截客户端请求并将请求转发到相应的Action类中来处理 4.创建 ...