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. [转]C# 系统应用之鼠标模拟技术及自动操作鼠标

    原文网址: C# 系统应用之鼠标模拟技术及自动操作鼠标        游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C# ...

  2. [Python] numpy.mat

    numpy.mat numpy.mat(data, dtype=None) Interpret the input as a matrix. Unlike matrix, asmatrix does ...

  3. 本地yum源快速创建

    1.建立挂载目录mkdir /rui 2.挂载iso到新建的/rui目录

  4. elk6.22

    启动错误: 参考网站:https://blog.csdn.net/feinifi/article/details/73633235?utm_source=itdadao&utm_medium= ...

  5. HTML5 ES6 语法基础

    // 解构赋值 let [a, b, c, [s,e],d] = ["aa", "bb", "cc", [12, 23], "dd ...

  6. css属性 writing-mode 改变文字书写方向

    作为IE的私有属性之一,IE5.5率先实现了 writing-mode ,后期被w3c采纳成标准属性: #test{ -webkit-writing-mode: vertical-rl;/*horiz ...

  7. is not on any development teams

    is not on any development teams 1)账号正在申请中 2)申请成功后的账号? 加了3个账号,都是这样子的. 1:Xcode>Window> "Org ...

  8. CentOS 7 Tomcat安装

    官网: http://tomcat.apache.org/download-80.cgi 下 1.载zip包 >wget http://mirrors.hust.edu.cn/apache/to ...

  9. 文字折行不折行 css

    white-space : 1. normal  默认值 ,文字自动换行.               2. pre 使用<pre>标签形式,表示元素.                 * ...

  10. vue深入了解组件——处理边界情况

    一.访问元素&组件 在绝大多数情况下,我们最好不要触达另一个组件实例内部或手动操作DOM元素.不过也确实在一些情况下做这些事情是合适的. 1.1 访问根实例 在每个 new Vue 实例的子组 ...