Largest Rectangle in a Histogram

http://acm.hdu.edu.cn/showproblem.php?pid=1506

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23458    Accepted Submission(s): 7362

Problem 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
4 1000 1000 1000 1000
0
 
Sample Output
8
4000
 
Source

单调栈经典题

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std; struct sair{
long long v;
int pos;
}a[]; int main(){
std::ios::sync_with_stdio(false);
int n;
while(cin>>n){
if(!n) break;
for(int i=;i<=n;i++){
cin>>a[i].v;
a[i].pos=i;
}
stack<sair>st;
long long ans=;
long long cnt,pos;
for(int i=;i<=n;i++){
if(st.empty()){
st.push(a[i]);
ans=max(ans,st.top().v);
}
else{
while(!st.empty()&&st.top().v>=a[i].v){
cnt=st.top().v;
pos=st.top().pos;
st.pop();
if(st.empty()){
ans=max(ans,cnt*(i-));
}
else{
ans=max(ans,cnt*(i--st.top().pos));
}
}
st.push(a[i]);
}
}
int Last;
if(!st.empty()){
Last=st.top().pos;
}
while(!st.empty()){
cnt=st.top().v;
pos=st.top().pos;
st.pop();
if(st.empty()){
ans=max(ans,cnt*n);;
}
else{
ans=max(ans,cnt*(Last-st.top().pos));
}
}
cout<<ans<<endl;
}
system("pause");
}
/*
6 2 5 2 5 5 2
5 1 2 3 4 4
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
3 2 1 2
4 2 2 5 5
*/

Largest Rectangle in a Histogram(附上几组测试数据)的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  2. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  3. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

  4. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  5. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  6. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

  7. Largest Rectangle in a Histogram

    2107: Largest Rectangle in a Histogram Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 777  Solved: 22 ...

  8. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  9. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. [原]System.IO.Path.Combine 路径合并

    使用 ILSpy 工具查看了 System.IO.Path 类中的 Combine 方法 对它的功能有点不放心,原方法实现如下: // System.IO.Path /// <summary&g ...

  2. Jira 迁移工作

    JIRA: 1.直接同步应用服务: rsync -aSvHz --progress --delete atlassian root@192.168.1.243:/opt/atlassian 2. 同步 ...

  3. 1085 Perfect Sequence (25 分)

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

  4. IaaS,PaaS,SaaS 的区别和联系

    原文:http://www.ruanyifeng.com/blog/2017/07/iaas-paas-saas.html 越来越多的软件,开始采用云服务. 云服务只是一个统称,可以分成三大类. Ia ...

  5. Map 合并

    比如说 qq.com 100 163.com  90 QQ.COM 10 Qq.Com  5 …… 如果统计的话,需要忽略大小写的,即 QQ邮箱总共是100+10+5,怎么写? 其实这个应该不难的,就 ...

  6. 启动 node 文件时附带参数

    cmd: node app.js hello app.js var args = process.argv; console.log(args);//[ 'C:\\Program Files\\nod ...

  7. Latex Error:‘acmart.cls’ not found 解决方案:

    windows下latex编译ACM论文模板时,出现Latex Error:‘acmart.cls’ not found,解决方案:  首先cd至模板所在目录下,然后运行以下命令:  tex acma ...

  8. MySQL中使用BIT属性

    如果是组合类型,用bit比较好,有那个类型,就将那以为设为1即可.不然还有将所有类型的组合求出来用map来存对应数字. 用bit,即省空间又方便. 注意用bit不能直接在记录里面直接填数据,要通过sq ...

  9. NRF51822之DFU使用手机升级

    演示的工程是 [application]    nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble_app_hrs\pca10028\s110_w ...

  10. Activity服务类-1 DynamicBpmnService服务类

    这个服务是5.19版本后新增的一个服务,和RepositoryService的作用相似,都是与流程定义有关,但是却完全不同.从名字上来看是动态的BPMN服务,看里面的方法都是改变流程的相关属性.这个方 ...