hdu1506——Largest Rectangle in a Histogram
Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12019 Accepted Submission(s): 3326
rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:

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.
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.
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
8
4000
每一块木板的作用范围就夹在左右两边分别比他矮的木板之间。所以我们能够迭代地预处理出这两块木板的位置,之后仅仅须要for一遍就可以得到最大面积
一開始用递归的写法一直错,后来干脆改成非递归的了
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = 100010;
__int64 h[N];
int lp[N];
int rp[N];
int n; int main()
{
while (~scanf("%d", &n), n)
{
__int64 ans = 0;
for (int i = 1; i <= n; ++i)
{
scanf("%I64d", &h[i]);
ans = max(ans, h[i]);
}
lp[1] = 1;
rp[n] = n;
for (int i = 2; i <= n; ++i)
{
int p = i;
while (p > 1 && h[i] <= h[p - 1])
{
p = lp[p - 1];
}
lp[i]= p;
}
for (int i = n - 1; i >= 1; --i)
{
int p = i;
while (p < n && h[i] <= h[p + 1])
{
p = rp[p + 1];
}
rp[i]= p;
}
/* for (int i = 1; i <= n; ++i)
{
printf("%d ", lp[i]);
}
printf("\n");
for (int i = 1; i <= n; ++i)
{
printf("%d ", rp[i]);
}
printf("\n");*/
for (int i = 1; i <= n; ++i)
{
int l = lp[i];
int r = rp[i];
ans = max(ans, (r - l + 1) * h[i]);
}
printf("%I64d\n", ans);
}
return 0;
}
hdu1506——Largest Rectangle in a Histogram的更多相关文章
- hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu1506 Largest Rectangle in a Histogram
Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...
- HDU1506 Largest Rectangle in a Histogram (动规)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- NYOJ-258/POJ-2559/HDU-1506 Largest Rectangle in a Histogram,最大长方形,dp或者单调队列!
Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题.. ...
- 【题解】hdu1506 Largest Rectangle in a Histogram
目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的 ...
- 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 ...
- HDU1506 ( Largest Rectangle in a Histogram ) [dp]
近期情绪太不稳定了.可能是由于在找实习这个过程碰壁了吧.第一次面试就跪了,可能是我面的是一个新公司,制度不完好,我感觉整个面试过程全然不沾编程,我面试的还是软件开发-后来我同学面试的时候.说是有一道数 ...
- 【单调栈】hdu1506 Largest Rectangle in a Histogram
单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是 ...
- HDU1506 Largest Rectangle in a Histogram(算竞进阶习题)
单调栈裸题 如果矩形高度从左到右是依次递增,那我们枚举每个矩形高度,宽度拉到最优,计算最大面积即可 当有某个矩形比前一个矩形要矮的时候,这块面积的高度就不能大于他本身,所以之前的所有高于他的矩形多出来 ...
随机推荐
- hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))
链接: huangjing 题目:中文题目 思路: 1:这个题目假设去掉那个距离大于d的条件,那么必定是一个普通的LIS.可是加上那个条件后就变得复杂了.我用的线段树的解法. . .就是採用延迟更新的 ...
- UVa-Palindromes
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [linux]chown和chmod命令
chown chown命令是将指定文件的拥有者改为指定的用户或组 例如: chown mail:mail test.log,把test文件指定拥有者和组都为mail chown -R mail:mai ...
- poj1243(经典dp)
题目链接:http://poj.org/problem?id=1243 题意:让你猜一个物品的价格,猜低了或者猜高了都会提示你.G,L,表示你有G次机会猜一个数,如果猜错了,G会减少1次,如果你的错误 ...
- hdu1503(最长公共子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意:由两个字符串构造出另一个字符串,该字符串包含前两个字符串(按字符顺序,但不一定连续),使该 ...
- 阿里云免费试用之体验——阿里云serverECS试用心得
自上次參加了阿里云的开发人员大会回来 心里就一直惦记着阿里云 由于曾经各种各样什么的server也用了不少 年前開始接触阿里云 一直没有给予很多其它的关注 參加了这次的开发人员大会后 就想更进一步的了 ...
- Datameer for Hadoop Solution
Hadoop promises to become a ubiquitous framework for largescale business intelligence, but right now ...
- gcc的bug? c++模板类中友元函数的訪问权限问题
原文地址:http://stackoverflow.com/q/23171337/3309790 在c++中,模板类中能够直接定义一个友元函数.该函数拥有訪问该模板类非public成员的权限. 比方: ...
- deinstall oracle 11g on linux
deinstall oracle 11g on linux From 11gR2, oracle provide us an deinstall tool. With that now we ca ...
- linux网络编程学习笔记之三 -----多进程并发服务端
首先是fork()函数.移步APUE 8.3. 比較清晰的解释能够參考http://blog.csdn.net/lingdxuyan/article/details/4993883和http://w ...