栈的运用-poj2559
来源:互联网 发布:英语汉语发音区别知乎 编辑:程序博客网 时间:2024/06/03 19:08
Language:
Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12749 Accepted: 4117
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.
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 34 1000 1000 1000 10000
Sample Output
84000#include<iostream>#include<set>#include<map>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;const int MAX=100010;int n,h[MAX];int L[MAX],R[MAX];int st[MAX];void solve(){ int t=0; for(int i=0;i<n;i++) { while(t>0&&h[st[t-1]]>=h[i]) t--; L[i]=t==0?0:(st[t-1]+1); st[t++]=i; } t=0; for(int i=n-1;i>=0;i--) { while(t>0&&h[st[t-1]]>=h[i]) t--; R[i]=(t==0?n:st[t-1]); st[t++]=i; } LL res=0; for(int i=0;i<n;i++) res=max(res,(LL)h[i]*(R[i]-L[i])); cout<<res<<endl;}int main(){ //freopen("in.txt","r",stdin); while(cin>>n,n) { for(int i=0;i<n;i++) scanf("%d",&h[i]); solve(); } return 0;}
- 栈的运用-poj2559
- 萌萌的单调栈 : poj2796 , poj2559 ,
- poj2559模拟单调栈
- poj2559栈应用
- poj2559单调栈
- poj2559(单调栈)
- POJ2559单调栈
- poj2559
- poj2559
- POJ2559
- POJ2559——Largest Rectangle in a Histogram(栈的应用)
- 2017sdut省赛选拔(1)--poj2559(单调栈的应用)
- poj2559 我要做的足够 强大
- poj2559(单调栈)最大矩形面积
- POJ2559 Largest Rectangle in a Histogram [单调栈]
- POJ2559 Largest Rectangle in a Histogram(单调栈)
- POJ2559--Largest Rectangle in a Histogram(栈)
- 单调栈 poj2559 Largest Rectangle in a Histogram
- 第七周
- 后台服务器开发总结
- 什么是虚拟机?
- UVa 438 The Circumference of the Circle (计算几何)
- Farey序列与Stern-Brocot树
- 栈的运用-poj2559
- excel 锁定第一行
- 六大原则
- 关于as400的一些讲解
- 数据库--sql作业,执行自动备份的存储过程
- 读书笔记:java线程_1
- ABAP的一些命令处理(CHECK、STOP、EXIT、continue、return、reject)
- svn日志信息未修改,或未指定解决访法
- RCP创建一个向导页 (WizardPage)