题目链接

对栈的一种灵活运用吧算是,希望我的注释写的足够清晰。。

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL; const int N=;
int Stack[N]; //Stack[]为单调栈(即每次能入栈的元素值必比栈顶元素(若存在)要大)
int len[N]; //len[]与Stack[]同步 ,记录的是从Stack[]中对应元素起向左最大可延伸的宽度
int n;
LL top,ans,h; void print()
{
printf("top=%d, ans=%d\n",top,ans);
printf("\tStack:");
for(int i=;i<=top;i++)
printf("%6d",Stack[i]);
puts("");
printf("\t len:");
for(int i=;i<=top;i++)
printf("%6d",len[i]);
puts("");
} int main()
{
while(scanf("%d",&n)&&n)
{
memset(Stack,,sizeof(Stack));
memset(len,,sizeof(len));
top=-,ans=;
for(int i=; i<=n; i++)
{
if(i<n) scanf("%lld",&h);
else h=-; //用作结束标记
if(top<||Stack[top]<h)
{
Stack[++top]=h; // h入栈
len[top]=; // 显然新入栈元素比原栈顶元素大,此时向左最大延伸“1”宽度
} // “1”可根据具体题目进行修改~
else // if(top>=0&&Stack[top]>=h
{
int l=;
while(Stack[top]>=h&&top>=)
{
ans=max(ans,(LL)(len[top]+l)*Stack[top]); //更新ans
l+=len[top--]; //Stack[]和len[]同时将栈顶元素弹出
} //循环结束后, top<0||Stack[top]<h
if(h>)
{
Stack[++top]=h; // h入栈
len[top]=l+; // 以top为起点的最长连续(h>=Stack[top])的区间长度
}
}
// print();
}
printf("%lld\n",ans);
}
}

hdu 1506:Largest Rectangle in a Histogram 【单调栈】的更多相关文章

  1. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  2. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

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

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

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

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

  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 1506 Largest Rectangle in a Histogram(区间DP)

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

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

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

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

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

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

随机推荐

  1. python-zx笔记11-测试压力管理

    一.添加测试用例 calculator.py class Math: def __init__(self,a,b): self.a = int(a) self.b = int(b) def add(s ...

  2. 字符串(一):char 数组

    字符串使用方法整理 系列: 字符串(一):char 数组 字符串(二):string 1. 声明 如下是一个例子(=> 表示表达式等价): char a[20] = "abcd&quo ...

  3. html+js(swiper.js)+css左右滑动切换页面效果,适配移动端

    demo: 截图: 结构:1.swiper-progress.html2.css文件夹 -swiper.css -swiper.min.css 3.js文件夹 -swiper.min.js -swip ...

  4. 关于JS的面向对象的思考和总结

    面向对象编程的概念和原理 1.面向对象编程是什么 它是用抽象的方式创建基于现实世界模型的编程模式(将数据和程序指令组合到对象中) 2.面向对象编程的目的 在编程中促进更好的灵活性和可维护性,在大型软件 ...

  5. JAVA数组的toString()方法不能直接输出数组内容?

    问题描述:我定义了一个类,类名是Job,当我输出Job.toString()是可以按我重载的toString方法输出的,但是如果输出jobs[]这个数组时,只会输出[Lmodel.Job;@45e22 ...

  6. day16—正是Github,让社会化编程成为现实。

    转行学开发,代码100天——2018-04-01 今天简单了解了一下GitHub的使用. 对于GitHub,在很多年前开始写程序的时候就频繁听到,也早早地注册之后看了真容.但是由于自己一直未产出较大型 ...

  7. Delphi 快速读取TXT 指定行的数据

    http://blog.csdn.net/MichaelJScofield/article/details/41869785 Delphi 快速读取TXT 指定行的数据 分类:Delphi个人挫品 ( ...

  8. idhttpserver 下载文件

    procedure TForm30.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AR ...

  9. 函数式编程filter和map的区别

    # b = filter(lambda x:x>5,[1,2,3,4,5,6,7]) # print(list(b)) def filters(x): if x > 5: return x ...

  10. Go错误处理机制及自定义错误

    错误处理机制: 先看一段代码:看看输出什么? package mainimport "fmt" func test() { num1 := 10 num2 := 0 res := ...