单调栈(POJ2559)
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
题意图给的很明显了,手写栈代码如下
#include<iostream>
#include<cstring>
using namespace std;
int a[],s[],w[];
int main()
{
int n,x;
long long ans;
while()
{
int p=;
ans=;
memset(a,,sizeof(a));
memset(w,,sizeof(a));
memset(s,,sizeof(s));
cin>>n;
if(n==)
break;
for(int i=;i<=n;i++)
cin>>a[i];
a[n+]=p;
for(int i=;i<=n+;i++)
{
if(a[i]>s[p])
{
s[++p]=a[i];
w[p]=;
}
else
{
int width=;
while(s[p]>a[i])
{
width=width+w[p];
ans=max(ans,(long long )width*s[p]);
p--;
}
s[++p]=a[i],w[p]=width+;
}
}
cout<<ans<<endl;
}
}
STL代码
#include<iostream>
#include<cstring>
#include<stack>
using namespace std; int main()
{
int n,x;
long long ans;
while()
{
int p=;
ans=;
cin>>n;
if(n==)
break;
stack< pair <int ,int > >s;
s.push(make_pair(,));
for(int i=;i<=n+;i++)
{
if(i==n+)
x=;
else
cin>>x;
if(x>s.top().first)
s.push(make_pair(x,));
else
{
int w=;
while(s.top().first>x)
{
w=w+s.top().second;
ans=max(ans,(long long )w*(s.top().first));
s.pop();
}
s.push(make_pair(x,w+));
}
}
cout<<ans<<endl;
}
}
单调栈(POJ2559)的更多相关文章
- POJ2559 Largest Rectangle in a Histogram —— 单调栈
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Lim ...
- [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」
Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...
- ☆ [POJ2559] Largest Rectangle in a Histogram 「单调栈」
类型:单调栈 传送门:>Here< 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题思路 单调栈的经典题 显然,最终的子矩形高度一定和某一个矩形相等(反证).因此一 ...
- poj2559/hdu1506 单调栈经典题
我实在是太菜了啊啊啊啊啊 到现在连个单调栈都不会啊啊啊 写个经典题 #include<cstdio> #include<algorithm> #include<cstri ...
- poj2559 Largest Rectangle in a Histogram(单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- POJ2559 Largest Rectangle in a Histogram 单调栈
题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于 ...
- poj 2059 单调栈
题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元 ...
- poj1964最大子矩阵 (单调栈加枚举)
题目传送门 题目大意: 一个矩阵中,求F组成的矩阵的面积,(答案乘以三). 思路:n如果是小于100的,就可以通过前缀和,然后三重循环暴力找,和poj1050很像,但由于是1000,就不可以了,时间复 ...
- POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈
POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...
- 51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1158 1158 全是1的最大子矩阵 基准时间限制:1 秒 空 ...
随机推荐
- VMware vCenter Server6.0安装及群集配置介绍
在本项目中,将在VMware Workstation 模拟的Windows Server 2008 R2虚拟机中安装VMware vCenter Server ,并且使用vCenter Server捆 ...
- C语言程序转汇编代码
最近在学着写bootloader,由于汇编太繁杂,希望可以使用C语言完成一部分,然后转成NASM汇编代码,经过摸索,最终找到了一个解决方案,记录于此,留作参考. 核心步骤 使用gcc编译得到.o文件 ...
- JavaScript的算数,赋值,比较和逻辑运算符
类似a=1+1这样的表达式称为运算符,js的运算符分为算数,赋值,比较和逻辑运算符:常见的算数有:+ - * / %(加减乘除,取模),比方说5/4=4*1+1:5%4=1,js算数顺序:从左往右,先 ...
- Windows、Linux之间传输文件的几种方式
常见的文件传输协议有ftp.sftp,sftp就是在ftp的基础上对传输的数据进行了加密. ftp速度快,sftp速度略慢但安全性高. ftp默认使用21端口,sftp默认使用22端口. 我使用的是C ...
- Android应用第一次启动时的欢迎界面制作
原理是这样,我们在SharedPreferences中存储一个int型数据,用来代表第几次登录,每次启动时都读取出来判断是不是第一次启动,然后依次判断是否要显示欢迎界面, 具体实现如下: 设置一个欢迎 ...
- vmware进程,虚拟机NAT模式配置固定ip,访问外网与ping通主机
vmware进程杀不掉 在使用vmware虚拟机时,如果强制结束vmware进程,可能会发现在资源监视器中有一个vmware-vmx.exe进程始终关不掉,获得管理员权限去杀或者重启都没有用,然后正常 ...
- 剑指offer-面试题58_2-左旋转字符串-字符串
/* 题目: 将字符串的前sep个字符转移到字符串尾部. */ /* 思路: 更好的方法: 先翻转前sep个字符,再翻转后面的字符,最后全体翻转. */ #include<iostream> ...
- 转:静态链表及其创建(C语言实现)
http://c.biancheng.net/view/3339.html 折半插入排序算法(C语言代码实现) http://c.biancheng.net/view/3440.html 二叉树层次遍 ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- PTA 邻接表存储图的广度优先遍历
试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ) 其中LGraph是邻接表存储的 ...