V - Largest Rectangle in a Histogram HDU - 1506
两种思路:
1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离加上栈顶元素到第二个栈顶元素的距离。
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1E5+;
ll arr[N];
int main(){
ll n;
while(cin>>n,n){
ll ans=;
stack<ll >st;
for(ll i=;i<=n;i++) cin>>arr[i];
arr[n+]=;
for(ll i=;i<=n+;i++){
if(st.empty()||arr[st.top()]<=arr[i]) st.push(i);
else {
ll c=st.top();
while(st.size()&&arr[c]>arr[i]){
st.pop();
if(st.empty()) ans=max(ans,arr[c]*(i-));
else {
ans=max(ans,(i-st.top()-)*arr[c]);
c=st.top();
}
}
st.push(i);
}
}
cout<<ans<<endl;
}
return ;
}
2 dp
维护两个数组left和right,left[i]表示元素i向大于当前元素向左的最大连续延伸。right[i]同理。
转移方式:j=left[j]-1,j=right[j]+1。然后遍历每个元素ans=max(ans,(right[i]-left[i]+1)*arr[i])
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1E5+;
ll arr[N];
ll left1[N];
ll right1[N];
int main(){
ll n;
while(scanf("%lld",&n)!=EOF&&n){
for(ll i=;i<=n;i++){
scanf("%lld",&arr[i]);
left1[i]=right1[i]=i;
}
for(ll i=;i<=n;i++){
ll j=i;
while(j>=&&arr[i]<=arr[j]) j=left1[j]-;
left1[i]=j+;
}
for (ll i=n-; i>=; i--) {
ll j=i;
while (j<=n && arr[i]<=arr[j]) j=right1[j]+;
right1[i]=j-;
}
ll ans=;
for(ll i=;i<=n;i++){
ans=max(ans,(right1[i]-left1[i]+)*arr[i]);
}printf("%lld\n",ans);
}
return ;
}
V - Largest Rectangle in a Histogram HDU - 1506的更多相关文章
- Largest Rectangle in a Histogram HDU - 1506 (单调栈)
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
- Day8 - C - Largest Rectangle in a Histogram HDU - 1506
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
- 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——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 ...
- 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 ...
随机推荐
- vue项目创建与使用
目录 复习 Vue项目环境搭建 Vue项目创建 pycharm配置并启动vue项目 vue项目目录结构分析 vue组件(.vue文件) 全局脚本文件main.js(项目入口) 改写 vue项目启动生命 ...
- 使用SparkSQL编写wordCount的词频统计
# 使用SparkSQL编写wordCount的词频统计 ## word.txt```hello hello scala sparkjava sql html java hellojack jack ...
- UVA129 Krypton Factor 困难的串 dfs回溯【DFS】
Krypton Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- vue - Babel之babel-polyfill、babel-runtime、transform-runtime
引言 babel默认只转换新的 JavaScript 语法,比如箭头函数.扩展运算(spread). 不转换新的 API,例如Iterator.Generator.Set.Maps.Proxy.Ref ...
- LoardPe与Import REC X64dbg脚本 脱壳 Upx
目录 LoardPe与Import REC X64dbg脚本 脱壳 Upx 一丶X64dbg调试器与脚本 1.1 起因 1.2 脚本的调试 1.3 Upx脱壳脚本 二丶LoardPe 内存Dump与I ...
- Arcgis License的安装及破解
1.双击LicenseManager安装目录下的Setup.exe. 2.点击“Next”. 3.选择“I accept the license agreement”,点击“Next”. 4.点击“C ...
- Git入门操作(一)
最近真正用到了Git,感觉还是需要好好整理一下最最基础用法,与萌新共享.^_^ 关于Git的基础介绍,这里不再赘述,下面撸代码了(主要是命令行的操作,属于linux操作系统的,可能没听过,但记住就好了 ...
- Mysql 随笔记录
Soundex 声音相似的 select * from demos where Soundex('title') = Soundex('标示'); Concat 拼接语句 select concat( ...
- Ubuntu系统查看命令命令使用方式
如:[gzip]命令,可执行: (tf) duanyongchun@hc1217:~/pycharm_projects /3DUNet-Pytorch /data$ gzip --help 输出: 由 ...
- centos7环境下安装nginx
安装所需环境 nginx是C语言开发,在Linux和windows环境上面都可以运行. 1.gcc安装 安装nginx需要将官网下载的代码进行编译,编译依赖gcc环境,如果没有gcc环境,需要先安装g ...