【单调栈】poj 2559 Largest Rectangle in a Histogram 直方图中的最大矩形 以及poj 3494

直方图中的最大矩形

时限:1000 MS 内存限制:65536K
提交材料共计: 33262 接受: 10837

描述
直方图是由在公共基线上对齐的一系列矩形组成的多边形。矩形的宽度相等,但可能有不同的高度。例如,左边的图显示由高度为2、1、4、5、1、3、3的矩形组成的直方图,单位为1是矩形的宽度:

在这里插入图片描述
通常,直方图用于表示离散分布,例如文本中字符的频率。请注意,矩形的顺序,即它们的高度,是很重要的。计算直方图中在公共基线上对齐的最大矩形的面积。右边的图显示了所描述的直方图的最大对齐矩形。

输入
输入包含几个测试用例。每个测试用例描述一个直方图,并以整数开头。n,表示由其组成的矩形数。你可以假设1<=n<=100000…然后跟着n整数h1.,hn,在哪里0<=hi<=1000000000…这些数字表示从左到右的直方图矩形的高度.每个矩形的宽度为1…零跟随最后一个测试用例的输入。

输出量
对于单行上的每个测试用例输出,指定直方图中最大矩形的面积。请记住,此矩形必须在公共基线处对齐。

样本输入
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

样本输出
8
4000

暗示
输入量大,推荐扫描。

题解:单调栈

#include<iostream>
#include <stack> 
using namespace std;
struct zft
{
	long long h;
	long long index;
	long long qian;
};
stack<zft>dan;
int main()
{
	int n;
	while (cin >> n&&n)
	{
		zft new1;
		long long max = 0, area = 0;
		for (int i = 0; i<n; i++)
		{
			cin >> new1.h;
			new1.index = i;
			while (!dan.empty() && new1.h<dan.top().h)
			{
				if (dan.size()>1)
				{
					area = dan.top().h*(new1.index - (dan.top().qian+1));
					if (max<area)max = area;
				}
				else
				{
					area = dan.top().h*(new1.index);
					if (max<area)max = area;
				}
				dan.pop();
			}
			if (!dan.empty())new1.qian = dan.top().index;
			dan.push(new1);
		}
		int right = dan.top().index;
		while (!dan.empty())
		{
			if (dan.size()>1)
				area = dan.top().h*(right - (dan.top().qian + 1) + 1);
			else area = dan.top().h*(right + 1);
			if (max<area)max = area; 
			dan.pop();
		}
		cout << max << endl;
	}
	return 0;
}

Largest Submatrix of All 1’s

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 9520 Accepted: 3410
Case Time Limit: 2000MS
Description

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input

The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

Output

For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

Sample Input

2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
Sample Output

0
4

不知为何这题就是超时,明明理论上可以的,写了两次都超时,答案也对,用的就是单调栈啊。。。烦。

#include<iostream>
#include<string.h>
#include <stack> 
using namespace std;
int matrix[2005][2005];
long long high[2005][2005];
struct zft
{
	long long h;
	long long index;
};
int main()
{
	int n, m;
	while (~scanf("%d",&m)!=EOF)
	{
		scanf("%d", &n);
		long long max = 0,area=0;
		zft dan[2005];
		for (int i = 0; i < m; i++)
		{
			for (int j = 0; j < n; j++)
			{
				scanf("%d", &matrix[i][j]);
				if (matrix[i][j] == 0)
					high[i][j] = 0;
				else if (i == 0)high[i][j] = 1;
				else
					high[i][j] = high[i - 1][j] + 1;
			}

			int top = 0;
			for (int j = 0; j < n; j++)
			{
				while (top > 0&&high[i][j] < dan[top].h)
				{
					if (top>1)
					{
						area = dan[top].h*(j - (dan[top - 1].index + 1));
						if (max<area)max = area;
					}
					else
					{
						area = dan[top].h*j;
						if (max<area)max = area;
					}
					top--;
				}
				dan[++top].h = high[i][j];
				dan[top].index = j;
			}
			while (top > 0)
			{
				if (top > 1)
				{
					area = dan[top].h*(n - 1 - (dan[top - 1].index + 1) + 1);
				}
				else area = dan[top].h*n;
				if (max < area)max = area;
				top--;
			}
		}
		printf("%lld",max);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值