Day8 - C - Largest Rectangle in a Histogram HDU - 1506
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.
InputThe 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.OutputFor 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 思路:单调队列板子题,注意在单调性出队的时候统计长度,将新入队的长度变长 提供一组数据 7 4 5 6 2 3 3 3 (14不是12
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int buf[maxm], q[maxm], len[maxm]; int main() {
ios::sync_with_stdio(false), cin.tie();
int n;
while(cin >> n && n) {
LL ans = ;
for(int i = ; i <= n; ++i) {
cin >> buf[i];
len[i] = ;
}
buf[n+] = ;
int l = , r = -, cnt;
bool flag = false;
for(int i = ; i <= n+; ++i) {
cnt = ; flag = false;
while(l <= r && buf[q[r]] > buf[i]) {
flag = true;
cnt += len[q[r]];
ans = max(ans, 1LL*cnt*buf[q[r]]);
r--;
}
q[++r] = i;
if(flag) len[i] = cnt+;
}
cout << ans << "\n";
} return ;
}
也可以用笛卡尔树来做,注意这里并不是严格的笛卡尔树,因为是有相同权值的节点的,一个点的长度就是他后裔的个数+自身(1),搜索时使用记忆化,不然会T
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int buf[maxm], q[maxm], Left[maxm], Right[maxm], length[maxm]; int getlen(int u) {
if(length[u]) return length[u];
int len = ;
if(Left[u]) len += getlen(Left[u]);
if(Right[u]) len += getlen(Right[u]);
return length[u] = len;
} int main() {
ios::sync_with_stdio(false), cin.tie();
int n;
while(cin >> n && n) {
LL ans = ;
for(int i = ; i <= n; ++i) Left[i] = Right[i] = length[i] = ;
for(int i = ; i <= n; ++i)
cin >> buf[i];
int top = ;
for(int i = ; i <= n; ++i) {
while(top && buf[q[top]] > buf[i]) {
Left[i] = q[top];
top--;
}
if(top)
Right[q[top]] = i;
q[++top] = i;
}
for(int i = ; i <= n; ++i) {
int len = ;
len = getlen(i);
ans = max(ans, 1LL*len*buf[i]);
}
cout << ans << "\n";
} return ;
}
好像dp也可以,兄弟题1505好像需要,补了再来
Day8 - C - 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 ...
- V - Largest Rectangle in a Histogram HDU - 1506
两种思路: 1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离 ...
- 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 使用心得---工作中一些关键点
1.自定义组件 使用 v-for 循环,最好另外多加上 v-bind:key="items_name",这是特殊用的:key,而不是普通的 :属性 例:<Uiroom> ...
- C语言-调试
1 格式化输出函数printf("%d %s",a,str):格式化控制符之间不能有“逗号”,可以用空格 1.1格式化输入函数scanf(“%d”,t)格式化控制符之间不能有空格 ...
- centos7 ip配置及ssh服务连接
一.配置ip /etc/sysconfig/network-scripts/ifcfg-ens33 ifcfg-ens33 这个是配置网卡的配置文件,名字可能不一样,大概为:ifcfg-网卡名 TYP ...
- linux 从一台机器复制文件到另一台linux机器上去
1.功能说明scp就是security copy,用于将文件或者目录从一个Linux系统拷贝到另一个Linux系统下.scp传输数据用的是SSH协议,保证了数据传输的安全,其格式如下:scp 远程用户 ...
- PAT T1013 Image Segmentation
krustral算法加并查集,按题给要求维护并查集~ #include<bits/stdc++.h> using namespace std; ; const int inf=1e9; i ...
- nginx 变量相关的map模块与split_clients模块及geo模块和geoip模块及keepalive介绍
map 模块指令默认编译进nginx的 Syntax: map string $variable { ... } # 表示string匹配到{}里的值或变量赋值给$variable Default: ...
- Caffe2 用户手册概览(Caffe2 Tutorials Overview)[1]
在开始之前,我们很感激你对Caffe2感兴趣,希望Caffe2在你的机器学习作品中是一个高性能的框架.Caffe2致力于模块化,促进深度学习想法和原型的实现. 选择你的学习路线 1. 使用一个现成 ...
- BurpSuite pro v2.0.11版
下载地址: 链接:https://pan.baidu.com/s/1CgXgK_lV2OWjRT364hvfog 提取码:gvxy BurpSuite介绍 借用一下吾爱破解文章中的描述信息:https ...
- c++拷贝构造函数(翁恺c++公开课[26-27]学习笔记)
这节课在p26.拷贝构造中讲的很清楚,建议大家耐心的去看下. 什么时候会发生拷贝构造: 对象之间的初始化赋值 使用对象作为变量进行函数传参(通常使用引用来传参从而减去不必要的拷贝构造,提高效率和代码健 ...
- 【PAT甲级】1009 Product of Polynomials (25 分)
题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...