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);

	}
}
时间: 2025-01-12 05:12:37

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

poj 2349 求MST中第S大的权值

题目大意: 有一些炮台,如果这个炮台有卫星接收器,那么任意两个有卫星接收器的炮台可以通信,不受距离限制:否者,两个炮台之间只能通过对讲机通信,这是受距离限制的.要买一种对讲机,用在需要的炮台上,要求所有炮台两两之间可以直接或者间接通信,问要买通信距离D至少为多少的对讲机可以满足要求. 有S个卫星接收器,那么就可以减少S-1个距离开销.要让D尽可能小,就让这S-1个距离开销最大,所以,想法就是,求这些点的最小生成树,然后把所选的边排序,第S大的边的权值就是所求.假如有4个点,2个卫星,那么最长的那

poj 2485 求最小生成树中 最大的一个权值

Sample Input 1 //T 3 //n0 990 692 //邻接矩阵990 0 179692 179 0Sample Output 692 prim 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using na

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

Sample Input 3aaa12aabaabaabaab0Sample Output Test case #12 23 3 Test case #22 2 //aa有2个a6 2 //aabaab有2个aab9 312 4 0  1  2 3 4 5 6 7 8 9 10 11 a  a b a a b  a a b a a b     的next数组为-1 0 1 0 1 2 3 4 5 6 7 8 9 1 #include<stdio.h> 2 #include<iostrea

Poj 2559 Largest Rectangle in a Histogram(柱形统计图中的最大矩形面积)

 给出一个柱形统计图中,求其中的最大矩形面积 做完这道题,搜了一下题解大部分基本都是单调栈......然而做之前并不知道这是什么,其实用递推也可以做这道题,理解起来比较容易. 用两个数组l,r记录当前坐标可以向左和向右延伸的最远位置的坐标,然后就是递推了. 初始时将l[i],r[i]的值置为i,即自己的坐标.这里拿l[i]举例: 从左向右扫描统计图,计算当前位置的l[i]时,如果h[i] > h[ l[i] - 1 ]的话,那么l[i] = l[ l[i]-1  ]. 然后对于每个位置,an

poj 3895 Cycles of Lanes 修改tarjan算法求图中最大环

题意: 给一个边权均为1的无向图,求图中的最大环. 分析: tarjan算法一般用来强连通分量,它依次访问图中的各个强连通分量,这题要求最大环,而环也是强连通分量的一部分,所以可以在每个点访问其他点时修改时间戳,达到每个环上时间戳连续的目的,这样当访问到一个栈中节点时就能直接更新最大环了.根据同样的思路,即使边权任意,也可求最大环或最小环. 代码: //poj 3895 //sep9 #include <iostream> #include <vector> #include &l

poj 1151 求矩形面积并 (线段树扫描线)

题意: 给出n个矩形的左下角和右上角坐标,求这n个矩形所构成的面积 思路: 线段树扫描线 这是第一次做到线段树扫描线,刚开始也不懂 如果不懂,可以看: http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 和 http://www.faceye.net/search/69289.html 我是看第一个链接弄懂的 然后学习了第二位的方法 代码上也有比较详细的注释,想可以帮到大家 code: #include<cstd

poj 2559 Largest Rectangle in a Histogram 栈

// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的是数据结构做,也可以递推做,目前只会数据结构的 // // 对于每个高度h,求一个左边界L和右边界R,分别表示的意义是 // L是下标为j的矩形的高度的hj小于当前h的最大的j的值.则根据定义 // 我们可以知道j到i之间的h都是大于当前的hi的. // R是下标为k的矩形的高度的hk大于当前h的最

每日一dp(1)——Largest Rectangle in a Histogram(poj 2559)使用单调队列优化

Largest Rectangle in a Histogram 题目大意: 有数个宽为1,长不定的连续方格,求构成的矩形中最大面积 /************************************************************************/ /* 思路1. 当前为n的面积如何与n-1相联系,dp[i][j]=max(dp[i-1][k]) , 0<k<=j 描述:i为方块个数,j为高度 但是此题目的数据对于高度太变态,h,1000000000 ,n,1

HDU 1506 &amp;&amp; POJ 2559 Largest Rectangle in a Histogram (单调队列)

题目链接:POJ 2559  Largest Rectangle in a Histogram 题目链接:HDU 1506  Largest Rectangle in a Histogram 题意:给出一串序列表示对应矩形的高度,求整个图中最大的矩形区域. 2, 1, 4, 5, 1, 3, 3 如图所示: 思路:每个矩形向左向右最大能扩张到的长度乘上他的高度,求最大值就是答案. 用单调队列维护序列递减,出队列的元素即是"极值"点 注意:要用int64. AC代码: #include&