HDU1506 Largest Rectangle in a Histogram 直方图中最大的矩形(单调栈/动态规划/笛卡尔树)

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 heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
 


Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

直方图是由在公共基线处对齐的一系列矩形组成的多边形。

矩形具有相等的宽度,但可以具有不同的高度。

例如,图例左侧显示了由高度为 2,1,4,5,1,3,32,1,4,5,1,3,3 的矩形组成的直方图,矩形的宽度都为 11

 

通常,直方图用于表示离散分布,例如,文本中字符的频率。

现在,请你计算在公共基线处对齐的直方图中最大矩形的面积。

图例右图显示了所描绘直方图的最大对齐矩形。

输入格式

输入包含几个测试用例。

每个测试用例占据一行,用以描述一个直方图,并以整数 nn 开始,表示组成直方图的矩形数目。

然后跟随 nn 个整数 h1hnh1,…,hn

这些数字以从左到右的顺序表示直方图的各个矩形的高度。

每个矩形的宽度为 11

同行数字用空格隔开。

当输入用例为 n=0n=0 时,结束输入,且该用例不用考虑。

输出格式

对于每一个测试用例,输出一个整数,代表指定直方图中最大矩形的区域面积。

每个数据占一行。

请注意,此矩形必须在公共基线处对齐。

题解

单调栈

这是当时学单调栈时的经典例题,单调栈维护的是一个从左向右递增的一个序列,
比较巧妙的地方在于每次将一个数放入栈,将大于这个数的数据弹出之后,

将最左边那个弹出的位置再次放入,将这个位置重新放入栈,

但要将这个位置的权值更改为这个放入的数 
当然为了保证每一个数都能弹出,在读入数据后在最后要加一个-1的值 

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const int N = 1e5+100;

int n;
int a[N];
void solve()
{
	for(int i=0;i<n;i++)cin>>a[i];
	a[n]=-1;
	stack<int>st;
	int res=0,ans=0;
	for(int i=0;i<=n;i++)
	{
		if(st.empty()||a[st.top()]<=a[i])
		{
			st.push(i);
		}else
		{
			while(!st.empty()&&a[st.top()]>a[i])
			{
				res=st.top();
				st.pop();
				ans=max(ans,(ll)(i-res)*a[res]);
			}
			
			st.push(res);
			a[res]=a[i];
			
		}
		
	}
	cout<<ans<<'\n';
}

signed main() {
	while(cin>>n) {
		if(n==0)break;
		solve();
	}
	return 0;
}

动态规划

这题还可以用动态规划的做法
对于每一个点预处理出左右边界
在预处理的过程中适当优化
比较简单,详见代码 

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const int N = 1e5+100;

int n;
int a[N];
int l[N],r[N];
void solve() {
	for(int i=1; i<=n; i++)cin>>a[i];
	for(int i=1; i<=n; i++) {
		l[i]=i;
		for (int j=i-1; j>=0; j=l[i]-1) {
			if(a[j]>=a[i]) {
				l[i]=l[j];
			} else break;
		}
	}
	for (int i=n; i>=1; i--) {
		r[i]=i;
		for (int j=i+1; j<=n; j=r[i]+1) {
			if(a[j]>=a[i]) {
				r[i]=r[j];
			} else break;
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		ans=max(ans,(ll)(r[i]-l[i]+1)*a[i]);
	}
	cout<<ans<<'\n';
}

signed main() {
	while(cin>>n) {
		if(n==0)break;
		solve();
	}
	return 0;
}

笛卡尔树

在学习了笛卡尔树之后发现这道题完全满足笛卡尔树的性质
对于一个小根堆的笛卡尔树
每一个结点,它的子树都是大于它的
且它和它的子树可以形成一个连续的区间
所以每一个结点可被计算的范围就是它的子树的大小
乘上结点的权值就是矩形的大小 

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const int N = 1e5+100;

ll m,ans;
struct node {
	int idx, val, par, ch[2];

	friend bool operator<(node a, node b) {
		return a.idx < b.idx;
	}

	void init(int _idx, int _val, int _par) {
		idx = _idx, val = _val, par = _par, ch[0] = ch[1] = 0;
	}
} tree[N];

int root, top, stk[N];
int cartesian_build(int n) {  
	for (int i = 1; i <= n; i++) {
		int k = i - 1;
		while (tree[k].val > tree[i].val) k = tree[k].par;
		tree[i].ch[0] = tree[k].ch[1];
		tree[k].ch[1] = i;
		tree[i].par = k;
		tree[tree[i].ch[0]].par = i;
	}
	return tree[0].ch[1];
}

int dfs(int x)
{
	if(!x)return 0;
	int sz=dfs(tree[x].ch[0]);
	sz+=dfs(tree[x].ch[1]);
	ans=max(ans,(ll)(sz+1)*tree[x].val);
	return sz+1;
}

void solve() {
	tree[0].init(0,0,0); 
	for(int i=1; i<=m; i++)
	{
		int x;
		cin>>x;
		tree[i].init(i,x,0);
	}
	root=cartesian_build(m);
	ans=0;
	dfs(root);
	cout<<ans<<'\n';
}

signed main() {
	while(cin>>m) {
		if(m==0)break;
		solve();
	}
	return 0;
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心刍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值