第一周 Largest Rectangle in a Histogram
| 
 Language: 
题目: 
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 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 follow n 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 Sample Output 8 Hint Huge input, scanf is recommended. 
Source  | 
思路:
我们把每个数字都看成一个宽度为1,长度为a[i]的小矩形,我们从左向右遍历每一个小矩形,然后以这个小矩形向左向右扩展,所能向左扩展的最大值(下标)用L[i]记录下来,所能扩张的最大值(下标)用R[i]记录下来,并把所能扩展最大的矩形面积a[i]*(R[i]-L[i])记录下来,每遍历一个小矩形就动态的经行更新最大值,然后最后输出最后的值即可。
便于理解的图文:
1) 刚开始栈中为空,压入下标0;然后当i=1时,2<=1不成立,下标出栈,栈变为空,计算此时面积res=2;

2)高度1,5,6是依次递增的,所以对应下标依次压入栈中;当i=4时,6>2,下标3出栈,所以计算此时的面积res=6,如图:

3)栈顶元素为2,其对应的高度为5>2(此时,还要和下标i=4的高度比较),所以,栈顶元素2出栈,计算面积为res=10;

4)因为栈顶元素为1,其对应的高度为1<2,满足条件,所以,将i入栈,直到i=6(为压入的0);此时栈中的元素为{1,4,5},有一个栈顶元素对应的高度大于i=6对应的高度,所以,5出栈,计算面积为3,其中最大面积为10;

5)栈顶元素为4,其对应高度为2>0,所以,2出栈,计算面积为4,最大面积为10;

6) 此时,栈顶元素为1,对应高度为1>0,所以,1出栈,因为1出栈以后,栈变为空,所以,计算面积的方式有变化,res=height[1]*6=6,最大面积为10。

代码1:(数组)
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
ll a[maxn];
ll L[maxn],R[maxn];
int st[maxn];//栈的大小
int main(){
int n;
while(cin>>n&&n){
int t=;
for(int i = ;i < n;i++)
scanf("%lld",&a[i]);
for(int i=;i<n;i++){
while(t>&&a[st[t-]]>=a[i]) t--;
L[i] = t==?:(st[t-]+);
st[t++] = i;
}
t = ;
for(int i=n-;i>=;i--){
while(t>&&a[st[t-]]>=a[i]) t--;
R[i] =t ==?n:(st[t-]);
st[t++] = i;
}
ll res = ;
for(int i = ;i<n;i++){
res = max(res,a[i]*(R[i]-L[i]));
}
printf("%lld\n",res); }
return ;
}
代码2:(栈)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5+;
ll h[maxn];
stack<int> s;
int main(){
int n;
while(cin>>n&&n){
memset(h,,sizeof(h));//每一组数据之前都需要初始化
while(!s.empty()) s.pop();//把栈中的元素清空
for(int i=;i<=n;i++)//i从1开始
scanf("%lld",&h[i]);
ll j=,res=;//h[]数组设为long long 类型后j也必须为long long型
while(j<=n+){//这里因为当j==n+1,是把栈中剩余的元素进行处理,例如样例中四个数全相同的时候还需最后处理一次,才能求出res的值
if(s.empty()||h[s.top()]<=h[j])
s.push(j++);
else{
ll t = s.top();
s.pop();
ll wid = s.empty()?(j-):(j-s.top()-);//这里因为第一个数组下标是从1开始的,所以当栈为空的时候,矩形的宽度必须为j-1
res = max(res,wid*h[t]);
}
}
cout<<res<<endl;
}
return ;
}
j-s.top()-1的含义:是所求矩形的宽,因为wid = L+R;
L = s.top()+1;//最左边的边界
R= j;//最右边的边界
这个栈的思路很巧,就是如果这个数符合栈中元素递增的规则或栈为空,则入栈
否则,就弹栈,这里是一个一个的弹栈操作,每弹出一个元素,就动态的更新res值,j不变,然后一直重复操作,直到栈顶元素<=要放入栈中的元素,那么就入栈
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std; typedef long long ll;
ll h[]; stack<int> s; int main()
{
int n;
while (scanf("%d", &n)&&n) {
memset(h, , sizeof(h));
while(!s.empty()) s.pop();
for (int i = ; i < n; i++) //i从0开始
scanf("%lld", &h[i]); ll j = , res = ;
while (j<=n) {
if (s.empty()||h[s.top()]<=h[j])
s.push(j++);
else {
ll t = s.top(); s.pop();
ll wid = s.empty()?j:(j-s.top()-);
res = max(res, h[t]*wid);
}
}
printf("%lld\n", res);
}
}
第一周 Largest Rectangle in a Histogram的更多相关文章
- poj 2559 Largest Rectangle in a Histogram (单调栈)
		
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
 - POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
		
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
 - hdu 1506 Largest Rectangle in a Histogram(单调栈)
		
L ...
 - Largest Rectangle in a Histogram HDU - 1506 (单调栈)
		
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
 - po'j2559  Largest Rectangle in a Histogram 单调栈(递增)
		
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29498 ...
 - [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
		
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
 - Largest Rectangle in a Histogram (最大子矩阵)
		
hdu 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common base line ...
 - NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!
		
Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...
 - [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」
		
Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...
 
随机推荐
- dfs与dp算法之关系与经典入门例题
			
目录 声明 dfs与dp的关系 经典例题-数字三角形 - POJ 1163 题目 dfs思路 解题思路 具体代码 dp思路 解题思路 具体代码 声明 本文不介绍dfs.dp算法的基础思路,有想了解的可 ...
 - Apache的虚拟主机功能(基于IP、域名、端口号)
			
Apache虚拟主机就是在一个Apache服务器上配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录. 主要有三种方法: 1.通过不同的IP地址 2.通过不同的域名 ...
 - Spring Cloud配置中心高可用搭建
			
本文通过config server连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行. 引入依赖 <dependencies> <dependency> ...
 - Python-装饰器的进阶  小知识点
			
⼀. 通⽤装饰器的回顾 开闭原则: 对增加功能开放. 对修改代码封闭 装饰器的作⽤: 在不改变原有代码的基础上给⼀个函数增加功能 通⽤装饰器的写法: def wrapper(fn): def inne ...
 - spark性能调优05-troubleshooting处理
			
1.调节reduce端缓冲区大小避免OOM异常 1.1 为什么要调节reduce端缓冲区大小 对于map端不断产生的数据,reduce端会不断拉取一部分数据放入到缓冲区,进行聚合处理: 当map端数据 ...
 - USACO 2014 US Open Dueling GPS's /// SPFA
			
题目大意: 给定n个点m条边的有向图 有两个GPS 分别认为 A[i]到B[i] 的一条边的花费是P[i].Q[i] 当当前走的边不是GPS认为的最短路上的边就会被警告 即两个GPS都不认为是最短路上 ...
 - vue对象侦测
			
http://blog.csdn.net/yihanzhi/article/details/74200618 数组:this.$set(this.arr,index,value)
 - LeetCode Array Easy 217. Contains Duplicate
			
Description Given an array of integers, find if the array contains any duplicates. Your function sho ...
 - teb教程5
			
跟随全局规划器 简介:本部分是关于如何配置局部规划器严格跟随全局规划,也包括调节在时优和路径跟随上的权衡. 1.先看一下via-points当前的优化行为:启动下面节点 roslaunch teb_l ...
 - grpc协议--客户端构造
			
由于服务端不在构造,已经构造完成不做构造 gRPC 接口名字为service,proto文件内有定义 1.本目录生成grpc文件 python -m grpc_tools.protoc -I. --p ...
 
			
		