首页 > 其他分享 >单调栈--Largest Rectangle in a Histogram

单调栈--Largest Rectangle in a Histogram

时间:2022-10-26 20:41:44浏览次数:38  
标签:-- Histogram ll histogram int empty st rectangles Rectangle


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: 

单调栈--Largest Rectangle in a Histogram_#include

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 4 1000 1000 1000 1000 0


Sample Output


8 4000


#include<iostream>
#include<stack>
#include<vector>

using namespace std;
typedef long long ll;

const int N=100005;
int n,L[N],R[N];
ll s[N],a[N];
stack<ll>st;

signed main()
{
while(scanf("%d",&n)!=EOF){
s[0]=0;
while(!st.empty())st.pop();
for(int i = 1;i <= n;i ++)
{
scanf("%lld",&a[i]);s[i]=s[i-1]+a[i];
}
for(int i=1;i<=n;i++){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty())L[i]=0;
else L[i]=st.top();
st.push(i);
}
while(!st.empty())st.pop();
for(int i=n;i>=1;i--){
while(!st.empty()&&a[st.top()]>=a[i])st.pop();
if(st.empty())R[i]=n;
else R[i]=st.top()-1;
st.push(i);
}
ll res=0;
int k;
for(int i = 1;i <= n;i ++){
ll tmp=(ll)a[i]*(s[R[i]]-s[L[i]]);
if(tmp>=res){
res=tmp;
k=i;
}
}
printf("%lld\n%d %d\n",res,L[k]+1,R[k]);
}
return 0;
}

 

标签:--,Histogram,ll,histogram,int,empty,st,rectangles,Rectangle
From: https://blog.51cto.com/u_13121994/5798366

相关文章

  • 单调栈--Feel Good
    DescriptionBillisdevelopinganewmathematicaltheoryforhumanemotions.Hisrecentinvestigationsarededicatedtostudyinghowgoodorbaddaysinfluentpe......
  • 并查集--翻译机的个数(顺丰2020年笔试)
    某学术会议上,一共有n个人参加,现在已知每个人会的语言(一个人可能不会任何语言)。现在有一种学习机,每一个学习机可以在会议期间使一个人暂时掌握一种自己不会的语言,问要使得任......
  • 最长非递减子序列--顺丰2020校招笔试题
    n的范围是[0,100000]DP版本(O(n^2))时间复杂度(LTE):#include<cstdio>#include<iostream>#include<algorithm>usingnamespacestd;#defineN100intmain(){intA[N],dp[N......
  • 字符串--移除k个数使得剩下的数最大
    有一十进制正整数,移除其中的K个数,使剩下的数字是所有可能中最大的。假设:字符串的长度一定大于等于K字符串不会以0开头 输入描述:一行由正整数组成的数字字符串,和......
  • DP--爬楼梯1
    题目描述前几个月放映的头号玩家简直火得不能再火了,作为一个探索终极AI的研究人员,月神自然去看了此神剧。由于太过兴奋,晚上月神做了一个奇怪的梦,月神梦见自己掉入了一个被施......
  • DP--字符串变换
    给定两个字符串,已知可以使用三种方式进行变换1.插入一个字符2.删除一个字符3.更改一个字符请设计一个算法,找到两个字符串之间的经历几次最小变换,可以字符串1转换成字......
  • DFS--同一个方向找出所有子字符串的个数
     字符迷阵是一种经典的智力游戏。玩家需要在给定的矩形的字符迷阵中寻找特定的单词。在这题的规则中,单词是如下规定的:1.在字符迷阵中选取一个字符作为单词的开头;2.选取......
  • map记录下标
    题目描述小云正在参与开发一个即时聊天工具,他负责其中的会话列表部分。会话列表为显示为一个从上到下的多行控件,其中每一行表示一个会话,每一个会话都可以以一个唯一正整数id......
  • DFS(全排列)--相同序号不能相邻
     小多想在美化一下自己的庄园。他的庄园毗邻一条小河,他希望在河边种一排树,共M棵。小多采购了N个品种的树,每个品种的数量是Ai(树的总数量恰好为M)。但是他希望任意......
  • N-叉树--遍历N-叉树所有从顶点到叶子节点的路径
    Shopee的OrangeDayShopee每个月都有属于大家的节日,每到这个时候,大家都会穿着橙色的T恤,吃着水果蛋糕,做着游戏。瞧,今天又是OrangeDay了,吃货小虾同学早早的来到现场,看看有没......