【单调栈】hdu1506 Largest Rectangle in a Histogram

单调栈的介绍及一些基本性质

http://blog.csdn.net/liujian20150808/article/details/50752861

依次把矩形塞进单调栈,保持其单增,矩形中的元素是一个三元组,存储其位置,高度,以及以其为高度的情况下,大矩形的左边界最多扩展到哪里。

每次将新的元素塞进栈的时候,其左边界就是其左侧第一个小于它的矩形的位置+1。

然后,每个矩形出栈的时候,记录其右边界为当前往栈里面塞的矩形的位置-1,然后更新答案即可。

注意最后把所有的矩形出栈,更新答案。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
struct data
{
	int l,h,p;
	data(const int &X,const int &Y,const int &Z)
	  {
	  	l=X;
	  	h=Y;
	  	p=Z;
	  }
	data(){}
};
stack<data>st;
int n,a[100010];
ll ans;
int main()
{
	while(1)
	  {
	  	scanf("%d",&n);
	  	if(!n)
	  	  break;
	  	ans=0;
		st.push(data(0,-1,0));
	  	for(int i=1;i<=n;++i)
	  	  scanf("%d",&a[i]);
	  	a[n+1]=-1;
	  	for(int i=1;i<=n+1;++i)
	  	  {
	  	  	while((!st.empty()) && a[i]<=st.top().h)
	  	  	  {
	  	  	  	ans=max(ans,(ll)(i-st.top().l)*(ll)st.top().h);
	  	  	  	st.pop();
	  	  	  }
	  	  	if(!st.empty())
	  	  	  st.push(data(st.top().p+1,a[i],i));
	  	  }
	  	cout<<ans<<endl;
	  }
	return 0;
}
时间: 2024-10-16 04:28:43

【单调栈】hdu1506 Largest Rectangle in a Histogram的相关文章

HDU1506 Largest Rectangle in a Histogram (动规)

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10873    Accepted Submission(s): 2969 Problem Description A histogram is a polygon composed of a sequence of rec

hdu1506——Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12019    Accepted Submission(s): 3326 Problem Description A histogram is a polygon composed of a sequence of rec

HDU1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26267    Accepted Submission(s): 8279 Problem Description A histogram is a polygon composed of a sequence of rect

[hdu1506 Largest Rectangle in a Histogram]笛卡尔树

题意:http://acm.hdu.edu.cn/showproblem.php?pid=1506 如图,求最大的矩形面积 思路: 笛卡尔树:笛卡尔树是一棵二叉树,树的每个节点有两个值,一个为key,一个为value.光看key的话,笛卡尔树是一棵二叉搜索树,每个节点的左子树的key都比它小,右子树都比它大:光看value的话,笛卡尔树有点类似堆,根节点的value是最小(或者最大)的,每个节点的value都比它的子树要小(或者大). 笛卡尔树的构造算法:从右链插入,同时维护右链的递增或递减序列

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

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

[题目链接]:click here~~ [题目大意]: A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that con

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

Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectang

POJ-2559 Largest Rectangle in a Histogram(单调栈)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22171   Accepted: 7173 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

POJ 2559 Largest Rectangle in a Histogram RMQ || 单调栈

题目链接:点击打开链接 题意就是求最大面积 枚举每个柱子作为起点 然后二分两边长度. 求个区间最值. #include<stdio.h> #include<iostream> #include<math.h> using namespace std; #define ll long long #define N 100100 inline bool rd(int &n){ int x = 0, tmp = 1; char c = getchar(); while