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

我想到的是第二种:最大的矩形,中间一定有个最矮的某个单位矩形。所以求出每一个包括矩形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. micropython陀螺仪控制舵机

    2018-03-1220:14:00 import pyb import time from pyb import Pin xlights = (pyb.LED(2),pyb.LED(3)) MO = ...

  2. spark查看stage和tasks信息

    spark提供了web-ui接口.外部命令等多种方法监视spark程序的执行状态.利用spark的监视功能,可以方便的查看spark应用程序执行的状态,具体包括:1)stage和tasks列表信息  ...

  3. Container Views

    https://developer.apple.com/documentation/uikit/views_and_controls Container Views Organize and pres ...

  4. day15-模块的基础及导入

    目录 模块 什么是模块 使用模块 import 循环导入问题 解决方案一 解决方案二 模块的搜索路径 Python文件的两种用途 包 导入包内包 导入包内包的模块 绝对导入与相对导入 绝对导入 相对导 ...

  5. Linux(Centos7) 设置静态IP

    关于虚拟机 这里使用Centos7为例,因为linux是安装在在虚拟机中,这里先看一下虚拟机的网络适配器: 这里我使用的NAT模式,接着配置虚拟机的虚拟网络: 这里主要看一下VMnet8的设置: 这里 ...

  6. 重启rsyncd

    systemctl  restart  rsyncd.service

  7. Python orm基础

    ORM 对象映射关系程序. 通过orm将编程语言的对象模型和数据库的关系模型建立映射关系,这样我们在使用编程语言对数据库进行操作的时候可以直接使用编程语言的对象模型进行操作就可以了,而不用直接使用sq ...

  8. java8 foreach不能使用break、countinue

    在学习1.8新特性的过程中,发现foreach中不可以使用break和countinue,然后我使用了return,结果如下图,对循环并没有影响. 百度一下,发现了一个不一样的回答 然后我就看了下源码 ...

  9. css--小白入门篇1

    一.引入 css用来描述html,学习css前我们先来学习html的基础标签的用法,再进入css的学习. 本教程面向小白对象,不会讲细枝末节深入的东西. 二.列表 列表有3种 2.1 无序列表 无序列 ...

  10. P1048 采药

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...