题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=1506

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
 题目大意:求最大的矩形面积;
题解:非递减单调栈,每次遇到一个小于栈内元素的值时,要出栈,出栈的同时记录面积,记录面积有点不太好理解....
面积的记录分为两种情况,我们假设当遇到元素arr[i]时,,arr[i]要大于栈顶元素。。。 我们取出栈顶元素x,然后再删除栈顶元素。若删除后栈为空,这说明从 从开始到 i ,取出 的元素是最小的,所以面积ans=arr[x]*(i-1)
若栈不为空,则说明从当前的栈顶元素的位置到 i,我们之前取出的arr[x]是最小的,即面积为ans=arr[x]*(i-st.top()-1)
(记得开long long)
AC code:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
typedef long long ll;
ll arr[N];
int main(){
int n;
while(cin>>n,n){
stack<int >st;
for(int i=;i<=n;i++) scanf("%lld",&arr[i]);
arr[n+]=;
long long ans=;
for(int i=;i<=n+;i++){
if(st.empty()||arr[i]>=arr[st.top()]){
st.push(i);
}
else {
int x=st.top();
while(st.size()&&arr[i]<arr[x]){
st.pop();
if(st.size()){
ans=max(ans,arr[x]*(i-st.top()-));
x=st.top();
}
else ans=max(ans,arr[x]*(i-));
}
st.push(i);
}
}
cout<<ans<<endl;
} return ;
}

Largest Rectangle in a Histogram 杭电1506的更多相关文章

  1. Largest Rectangle in a Histogram(HDU 1506 动态规划)

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

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

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

  3. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  4. uva 1506 Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram http://acm.hdu.edu.cn/showproblem.php?pid=1506 Time Limit: 2000/100 ...

  5. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  6. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

  7. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  8. 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 ...

  9. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

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

随机推荐

  1. 如何在Linux系统上安装nginx

      安装Nginx 下载Nginx 到官网http://nginx.org/下载对应nginx包,推荐使用稳定版本进入官网之后界面如下 点击download进行到下一页 然后下载所需要的版本(强烈建议 ...

  2. [线段树]Codeforces 339D Xenia and Bit Operations

    Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  3. Codeforce219C-Color Stripe

    E. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. 一文带你入门Java Stream流,太强了

    两个星期以前,就有读者强烈要求我写一篇 Java Stream 流的文章,我说市面上不是已经有很多了吗,结果你猜他怎么说:"就想看你写的啊!"你看你看,多么苍白的喜欢啊.那就&qu ...

  5. C语言自学网官方微信相关功能使用方法

    一.微信扫描关注微信公众账号(C语言自学网),我们会不定时更新关于编程的技术文章和相关资讯,了解大佬的成长之路. 二.回复“C语言学习资料”,将显示由我们精心整理的全套C语言学习资料的下载地址及链接. ...

  6. CodeForces - 1244D 树(一条链)的染色

    题意:给一个无向的无环的树,需要用三种颜色将他染色,相邻的三个点不能有重复的颜色.给出每个点染成每种颜色的花费,求最小的染色花费,如果给的图不能按要求染色,输出-1. 思路:只有三种颜色,相邻三个点还 ...

  7. 简单理解vertical-align属性和基线问题

    vertical-align属性主要用于改变行内元素的对齐方式,对于行内布局影响很大,如果不了解的话,我们开发调整样式的时候很容易出错. 网上关于这个属性的原理说得很是复杂,看一眼就让人觉得望而生畏, ...

  8. iOS isEqual

    如何重写 hash 方法 一个合理的 hash 方法要尽量让 hash 表中的元素均匀分布,来保证较高的查询性能. 如果两个对象可以被视为同一个对象,那么他们的 hash 值要一样. mattt 在文 ...

  9. Spring中应用的那些设计模式

    设计模式作为工作学习中的枕边书,却时常处于勤说不用的尴尬境地,也不是我们时常忘记,只是一直没有记忆. 今天,我们就设计模式的内在价值做一番探讨,并以spring为例进行讲解,只有领略了其设计的思想理念 ...

  10. ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径

    IViewLocationExpander API ExpandViewLocations Razor视图路径,视图引擎会搜索该路径. PopulateValues 每次调用都会填充路由 项目目录如下 ...