A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of 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.

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的更多相关文章

  1. 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 ...

  2. V - Largest Rectangle in a Histogram HDU - 1506

    两种思路: 1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离 ...

  3. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  4. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  5. 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 ...

  6. 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 ...

  7. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. pdf.js的使用 (3)真实项目分享

    需求:a.jsp页面要做一个pdf的预览功能,我采用layer.open()弹窗的形式来预览pdf 1.在a.jsp点击文件然后弹出窗口(其实是弹出b.jsp) var lay=layer.open( ...

  2. 吴裕雄 PYTHON 神经网络——TENSORFLOW 学习率的设置

    import tensorflow as tf TRAINING_STEPS = 10 LEARNING_RATE = 1 x = tf.Variable(tf.constant(5, dtype=t ...

  3. HtmlUnit-API的使用就介绍

    转自:https://www.cnblogs.com/luotinghao/p/3800054.html 网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源Htm ...

  4. springboot 打包成jar

    1.pom.xml配置 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  5. 安装插件报错error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++

    起因:学到多线程greenlet模块时,greenlet模块死活装不上,以为pycharm坏掉了,浪费了一下午. #pip3 install greenlet from greenlet import ...

  6. opencv人脸识别提取手机相册内人物充当数据集,身份识别学习(草稿)

    未写完 采用C++,opencv+opencv contrib 4.1.0 对手机相册内人物opencv人脸识别,身份识别学习 最近事情多,介绍就先不介绍了 photocut.c #include & ...

  7. 解决IDEA部署web项目时,jar包拷贝不全的问题

    原因 先前已部署过,输出目录有lib文件夹. 再次部署时,IDEA一检测,发现输出目录已经存在lib文件夹,认为已经拷贝过了,为节省时间,不再重新拷贝jar包,殊不知我们新添加了jar包. 于是我们新 ...

  8. Jenkins 定时备份插件 ThinBackup

    需求 公司的整个测试环境正式环境打包都是用的同一个Jenkins, 该Jenkins是搭建在内部的一台机器上,之前有一台机器的硬盘出了问题,为了安全起见,我们决定备份 Jenkins 的配置和数据. ...

  9. 001. 使用IDEA新建一个JAVA最简单的Spring MVC JAVAWEB程序

    1. 我们打开一个空的IDEA 2. 选择Java之后点击Next 3. 点击Next创建空白工程 4. 给工程取个名字,叫MYIDEA 5. 勾选之后,点击This Window按钮 6. 我们可以 ...

  10. Spring学习(三)

    DI (Dependency Injection) 1.依赖注入,组件之间的依赖关系由容器在运行期间决定.Ioc容器注入应用程序某个对象,它所需要的外部资源(包括对象,资源,常量数据). birthd ...