hdu 1506:Largest Rectangle in a Histogram 【单调栈】
对栈的一种灵活运用吧算是,希望我的注释写的足够清晰。。
#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 【单调栈】的更多相关文章
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)
题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...
- 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 ...
- HDU 1506 Largest Rectangle in a Histogram(区间DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...
- 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 ...
- 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 ...
- HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
随机推荐
- 学习日记9、easyui控件两次请求服务器端问题
<select id="BankCard" class="easyui-combobox" style="width: 600px;" ...
- hdu多校第二场1008(hdu6598) Harmonious Army 最小割
题意: 一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息是a,b,c,代表如果这两个人是两个战士,则组合技威力为a,一个战士一个法师,威力为b,其中b=a/4+ ...
- AtCoder Regular Contest 071 D - 井井井 / ###
题目:http://arc071.contest.atcoder.jp/tasks/arc071_b 题意: 有一个二维的平面,给你xn根竖线和ym根横线,问这些线围成的长方形(正方形)的面积和(要求 ...
- How to: Create a Windows Communication Foundation Client
How to: Create a Windows Communication Foundation Client To create a Windows Communication Foundatio ...
- HDU6702 ^&^(思维)
HDU6702 ^&^ 目标为 \((A \oplus C) \& (B \oplus C) = 0\) ,易得: \(A \& B=0\) 时:\(C = 1\) . \(A ...
- isset、empty 误区
isset() 常用来判定变量是否存在 但是当变量赋值为NUll时候,返回值一样是FALSE 而实际上变量是存在的,与实际情况不符,判定错误. empty() 用来判定变量或者类的属性值是否为等 ...
- Ceiling analysis
Course note: Coursera Machine learning by Andrew Ng, 2014, week 10: Application example: photo OCR ( ...
- 深入学习Keras中Sequential模型及方法
https://www.cnblogs.com/wj-1314/p/9579490.html
- 排序算法五:随机化快速排序(Randomized quicksort)
上一篇提到,快速排序的平均时间复杂度是O(nlgn),比其他相同时间复杂度的堆排序.归并排序都要快,但这是有前提的,就是假定要排序的序列是随机分布的,而不是有序的.实际上,对于已经排好的序列,如果用快 ...
- win10下装win7双系统安装教程
win10下装win7双系统安装教程 来源:www.laomaotao.org 时间:2017-02-13 10:15 新买的电脑预装了win10系统,但win10对于有些游戏兼容性不是很好,总是会出 ...