POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18942 | Accepted: 6083 |
Description

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
Output
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
Hint
Source
刚开始竟然没有注意到区间长度的累计,各种算错。
维护一个单增的单调栈,每步更新区间长度和答案即可。
/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
struct stk{
long long h,p;
}st[mxn];
int top;
int n;
long long ans;
int main(){
while(scanf("%d",&n) && n){
ans=;
int i,j;
int num;
for(i=;i<=n+;i++){
if(i>n)num=;//数据读完后清栈
else scanf("%I64d",&num);
if(num>=st[top].h) st[++top].h=num,st[top].p=;//计算新区间
else{
int len=;
while(top && num<=st[top].h){
len+=st[top].p;
ans=max(ans,len*st[top].h);
top--;
}
st[++top].p=len+;//累计长度
st[top].h=num;//更新高度
}
}
printf("%I64d\n",ans);
}
return ;
}
POJ 2559 Largest Rectangle in a Histogram的更多相关文章
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- 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 -- 动态规划
题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- 题解报告: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 ...
随机推荐
- 15Spring_AOP编程(AspectJ)_抛出通知
- java中从1970-1-1到当前时间之间的毫秒数转换为oracle date
java中System.currentTimeMillis()取到的是从1970-01-01 00:00:00.000到当前时间的毫秒数,一个long类型的值. 现在oracle数据库中某表中存取的是 ...
- Enabling CORS in WCF
Introduction This is an intermediate example of WCF as REST based solution and enabling CORS access, ...
- Matlab中的数据类型
Matlab中有15种基本数据类型,主要是整型.浮点.逻辑.字符.日期和时间.结构数组.单元格数组以及函数句柄等. 1.整型:(int8:uint8:int16:uint16:int3 ...
- [MetaHook] Load large texture from model
We need hook "GL_LoadTexture" engine function. GL_LOADTEXTURE_SIG from hw.dll(3266) engine ...
- linux实践——简单程序破解
一.运行login可执行程序,屏幕显示需要输入密码,随便输入一串字符,结果是Drop dead! 二.objdump -d login,对login进行反汇编,找到main函数,找到含有scanf的那 ...
- 信息安全系统设计基础exp_1
详见搭档20135322郑伟博客链接:http://www.cnblogs.com/zhengwei0712/
- Java中的String、StringBuffer以及StringBuilder的用法和区别
String String的构造方式有n种(据说n==11),常见的例举一二: String s1 = "hello world"; String s2 = new String( ...
- 第三章 Js变量的作用域和匿名函数
3.1 先看下面的事例: ①var temp=0; ②temp=0; 当js解析器检测到①这种情况的时候,解析器会为这个变量开辟一个内存空间,如果前面已经存在这个变量,就会把这个变量覆盖掉. 当解析器 ...
- (旧)子数涵数·PS——冷色调与LOMO
一.准备素材(我是从百度图库里下载的) 二.打开PS和素材 三.复制图层,快捷键Ctrl+J,并把原图层隐藏,只在副本上编辑(好习惯) 四.使用"匹配颜色"命令,增加"明 ...