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 ...
随机推荐
- JDBC statement的常用方法
Statement接口: 用于执行静态SQL语句并返回它所生成结果的对象. 三种Statement类: Statement: 由createStatement创建,用于发送简单的SQL语句(最好是不带 ...
- 《Web安全攻防 渗透测试实战指南》 学习笔记 (二)
Web安全攻防 渗透测试实战指南 学习笔记 (二) 第二章 漏洞环境及实践
- 【代码学习】PYHTON 元组
Python的元组与列表类似,不同之处在于元组的元素不能修改.也可进行分片 和 连接操作. 元组使用小括号,列表使用方括号. 一.访问元组 #coding=utf-8 Tuple = ('name', ...
- centos 8 cockpit系统监控
步骤: 1.激活cockpit服务 2.启动cockpit 3.查看cockpit服务是否启动 4.浏览器访问http://192.168.1.10:9090(用户名root,密码123) 5.查看系 ...
- springMVC的跳转
服务器内部跳转: return "forward:/forward/test1"; 或者 request.getRequestDispatcher(path).forward(r ...
- node vue 项目git 管理
push 上传到云的时候,依赖包及相关文件是不上传上去的, 所以每次克隆到本地后,node 项目运行前须要 npm install 安装对应依赖 vue 项目编译前也须要 npm install,安 ...
- techiediaries网站的Laravel 6系列教程
Laravel 6 Tutorial & New Features - Build a CRM [PART 1] Laravel 6 REST API CRUD Tutorial - Buil ...
- rem布局,在用户调整手机字体大小/用户调整浏览器字体大小后,布局错乱问题
一.用户调整浏览器字体大小,影响的是从浏览器打开的web页. 浏览器设置字体大小,影响浏览器打开的页面.通过js可控制用户修改字体大小,使页面不受影响. (function(doc, win) { / ...
- 题解【[Ynoi2012]NOIP2015洋溢着希望】
\[ \texttt{Preface} \] 第二道 Ynoi 的题,纪念一下. 这可能是我唯一可以自己做的 Ynoi 题了. \[ \texttt{Description} \] 维护一个长度为 \ ...
- Python 基础之正则之二 匹配分组,正则相关函数及表达式修饰符
四.匹配分组 [元字符] 分组符号 a|b 匹配字符a 或 字符b (如果两个当中有重合部分,把更长的那个放前面) (ab) 匹配括号内的表达式 ,将()作为一个分组 num 引用分组 ...