HDU 1506 && 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<stdio.h>
#include<string.h>
#define ll __int64
struct node
{
	ll l,r;
	ll sum;
};
struct node que[100010];
ll h[100010];
ll pre[100010],next[100010];//pre记录hi向左递减最远位置,next记录hi向右递减最远位置,

int main()
{
	ll n,i;
	ll head,tail;
	while(scanf("%I64d",&n)!=EOF,n)
	{
		memset(que,0,sizeof que);
		for(i=1;i<=n;i++)
		{
			scanf("%I64d",&h[i]);
			pre[i]=n;
			next[i]=1;
		}
		head=1,tail=0;
		for(i=1;i<=n;i++)
		{
			while(head<=tail && h[i]<que[tail].sum)//维护单调递减队列
			{
				pre[que[tail].l]=i-1;//出队列的元素是他扩张右边最远位置
				tail--;//弹出元素
			}
			que[++tail].sum=h[i];//入队列
			que[tail].l=i;//记录位置。
		}
		head=1,tail=0;
		for(i=n;i>=1;i--)
		{
			while(head<=tail && h[i]<que[tail].sum)
			{
				next[que[tail].r]=i+1;//出队列的元素是他扩张左边最远位置
				tail--;
			}
			que[++tail].sum=h[i];
			que[tail].r=i;
		}
		ll ans=0,temp;
		for(i=1;i<=n;i++)
		{
			temp=(pre[i]-next[i]+1)*h[i];
			if(temp>ans)
				ans=temp;
		}
		printf("%I64d\n",ans);
	}
	return 0;
}
时间: 2024-10-10 14:31:15

HDU 1506 && POJ 2559 Largest Rectangle in a Histogram (单调队列)的相关文章

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 (单调栈或者dp)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 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 栈

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

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

stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

题目传送门 1 /* 2 题意:宽度为1,高度不等,求最大矩形面积 3 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极限情况 4 st[]里是严格单调递增,若不记录的话还要O(n)的去查找L,R,用栈的话降低复杂度 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <algorithm> 9 #include &l

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

hdu 1506 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): 12554    Accepted Submission(s): 3509 Problem Description A histogram is a polygon composed of a sequence of rec

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

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 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 heigh