传送门

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 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.

Input

The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1<=n<=100000. Then follown integers h1,...,hn, where 0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

Output

For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

Sample Input

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output

8
4000

Hint

Huge input, scanf is recommended.

思路

因为每个矩形的宽都为1,高不等,要求拼接起来的矩形的面积的最大值,可以看做给定一列数,定义子区间的值为区间长度乘以区间最小值,求区间值最大为多少。直接枚举肯定T,所以以每个值为区间最小值,向左向右扩展延伸区间,然后更新最大值,也就是单调栈的思想。如果当前元素大于栈顶元素,那么这个元素是不能向前伸展的;如果当前元素小于栈顶元素,这个时候就要把栈中的元素一个一个弹出来,直到当前元素大于栈顶元素,对于弹出来的元素,它扩展到当前元素就不能向后伸展下去了,因此对于弹出来的元素这个时候就可以计算左右端点形成区间与最小值的乘积了,维护一个最大值就好了。
#include<stdio.h>
#include<string.h>
typedef __int64 LL;
const int maxn = 100005;
LL a[maxn],stack[maxn],left[maxn];

int main()
{
	int N;
	while (~scanf("%d",&N) && N)
	{
		LL res = 0,tmp;
		memset(stack,0,sizeof(stack));
		memset(left,0,sizeof(left));
		for (int i = 1;i <= N;i++)	scanf("%I64d",&a[i]);
		a[++N] = -1;             //手动加上“-1”,使得所有元素都能入栈出栈
		int top = 0;
		for (int i = 1;i <= N;i++)
		{
			if (!top || a[i] > a[stack[top-1]])
			{
				stack[top++] = i;
				left[i] = i;
				continue;
			}
			if (a[i] == a[stack[top-1]])	continue;

			while (top > 0 && a[i] < a[stack[top-1]])
			{
				top--;
				tmp = a[stack[top]]*((i-1)- (left[stack[top]]-1));
				res = res<tmp?tmp:res;
			}
			tmp = stack[top];
			stack[top++] = i;
			left[i] = left[tmp];
		}
		printf("%I64d\n",res);
	}
	return 0;
}

  

POJ 2559 Largest Rectangle in a Histogram(单调栈)的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  2. POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  3. PKU 2559 Largest Rectangle in a Histogram(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> ...

  4. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

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

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  6. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

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

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  8. POJ2559 Largest Rectangle in a Histogram —— 单调栈

    题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Lim ...

  9. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

随机推荐

  1. SignalR与ActiveMQ结合构建实时通信

    一.概述 本教程主要阐释了如何利用SignalR与消息队列的结合,实现不同客户端的交互 SignalR如何和消息队列交互(暂使用ActiveMQ消息队列) SignalR寄宿在web中和其他Signa ...

  2. 在CentOS上部署基于dnx/coreclr的ASP.NET 5应用程序

    在Ubuntu上写好了一个简单的ASP.NET 5应用程序,尝试将这个程序部署在没有mono环境的CentOS服务器上. 部署步骤如下: 1)安装libuv(KestrelHttpServer需要它) ...

  3. 突破自我,开源NetWorkSocket通讯组件

    前言 在<化茧成蝶,开源NetWorkSocket通讯组件>发表之后,收到大家很多个star,在此感谢!更可贵的是,一些网友提出了许多好建议,经过一些时间的思考,决定将NetworkSoc ...

  4. 从C++研发到前端工程师

    前言 伴随着今天收到了网易的前端offer,我的转行面试告一段落.能拿到网易的offer很意外,也弥补了去年网易校招被刷的遗憾.虽然从c++转行到前端不是一件很困难的事,但是也说不上轻松,反正我用了整 ...

  5. Java学习笔记(二二)——Java HashMap

    [前面的话] 早上起来好瞌睡哈,最近要注意一样作息状态.       HashMap好好学习一下. [定义] Hashmap:是一个散列表,它存储的内容是键值对(key——value)映射.允许nul ...

  6. 踩到一个Emit的坑,留个纪念

    重现代码: var dmFoo = new DynamicMethod("Foo", typeof(void), Type.EmptyTypes); var ilFoo = dmF ...

  7. promise的学习

    为了解决回调地狱的问题,所以出现了promise的设计思想. promise的三种状态: pending 等待状态 resolved 完成状态 rejected 拒绝状态 promise的三种状态,只 ...

  8. P值,“差异具有显著性”和“具有显著差异”

      P值是论文中最常用的一个统计学指标,可是其误用.解释错误的现象却很常见.因此,很有必要说明p值的意义.用法及常见错误.   P值指的是比较的两者的差别是由机遇所致的可能性大小.P值越小,越有理由认 ...

  9. Project Serve 2013部署方法

    在线版Project2013部署手册 服务器环境要求 系统:windows server 2008r2.windows server2012x64 Sharepoint 2013 内存至少16GB,最 ...

  10. iOS开发--利用MPMoviePlayerViewController播放视频简单实现

    一.MPMoviePlayerViewController和MPMoviePlayerController区分开,前者继承自NSObject,后者继承自UIViewController 二.MPMov ...