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 ...
随机推荐
- hdu1908 逆序对
题目链接:https://www.luogu.com.cn/problem/P1908 这个题不要以为拿到手就可以树状数组秒,本题的数据范围是1e9显然简单的树状数组是空间不够的,点个数有5e5,所以 ...
- 数据源管理 | 主从库动态路由,AOP模式读写分离
本文源码:GitHub·点这里 || GitEE·点这里 一.多数据源应用 1.基础描述 在相对复杂的应用服务中,配置多个数据源是常见现象,例如常见的:配置主从数据库用来写数据,再配置一个从库读数据, ...
- Redis学习笔记1-java 使用Redis(jedis)
一.远程操作Redis 1. 在windows环境下安装RedisDesktopManager 2. 打开RedisDesktopManager 3. Add New Connection 4. 右击 ...
- coding++:kafka问题:zookeeper is not a recognized option zookeeper参数不支持
– zookeeper is not a recognized option主要原因是 Kafka 版本过高,命令不存在. 使用新版本: ./bin/kafka-console-consumer.sh ...
- war 和 war exploded
IDEA 开发项目时,部署 tomcat 的 Deployment 选项出现: war 模式 可以称之为发布模式.先将 WEB 工程打成 war 包,然后再将其上传到服务器进行发布. war expl ...
- macbook中出现2003 - Can't connect to MySQL server on '127.0.0.1' (61 "Connection refused") 如何解决
第一步 关闭mysql服务: 苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server) 如果这种方法没有成功: 可以使用命令行 ...
- Python数据库之数据库基本操作
安装(基于centos) yum -y install mariadb mariadb-server # centos7版本 yum -y install mysql mysql-server #ce ...
- element的多文件上传
项目需求: 可上传多个文件 可删除 文件过大时用户输入可上传至其他网站,并将文件名和地址上传至本网站 问题点: 大文件用户输入内容无法合并到已上传文件的列表进行展示 上传多个大文件地址时前面已上传的大 ...
- CSS超链接样式,去除下划线等
控制超链接样式 链接的四种状态: a:link - 普通的.未被访问的链接 a:visited - 用户已访问的链接 a:hover - 鼠标指针位于链接的上方 a:active - 链接被点击的时刻 ...
- 1-1. OSS/ALSA 声卡的驱动与配置和 Madplay 嵌入式播放器的移植
报警子系统 一. OSS/ALSA 声卡的驱动与配置 声卡驱动中传统的OSS构架在02年被收购后即不开源,并且OSS的混音效果不好->因此ALSA构架孕育而生. ALSA(高级音频构架,目前应用 ...