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(算竞进阶习题)
单调栈裸题 如果矩形高度从左到右是依次递增,那我们枚举每个矩形高度,宽度拉到最优,计算最大面积即可 当有某个矩形比前一个矩形要矮的时候,这块面积的高度就不能大于他本身,所以之前的所有高于他的矩形多出来 ...
随机推荐
- MySQL聚合函数、控制流程函数(含navicat软件的介绍)
MySQL聚合函数.控制流程函数(含navicat软件的介绍) 一.navicat的引入:(第三方可视化的客户端,方便MySQL数据库的管理和维护) NavicatTM是一套快速.可靠并价格相宜的数据 ...
- R语言与数据分析之九:时间内序列--HoltWinters指数平滑法
今天继续就指数平滑法中最复杂的一种时间序列:有增长或者减少趋势而且存在季节性波动的时间序列的预測算法即Holt-Winters和大家分享.这样的序列能够被分解为水平趋势部分.季节波动部分,因此这两个因 ...
- Fast portable non-blocking network programming with Libevent
Fast portable non-blocking network programming with Libevent Fast portable non-blocking network prog ...
- UVA 11768 - Lattice Point or Not(数论)
UVA 11768 - Lattice Point or Not option=com_onlinejudge&Itemid=8&page=show_problem&categ ...
- Devstack: A copy of worked local.conf I'm sharing with you.
service_plugins = neutron.services.firewall.fwaas_plugin.FirewallPlugin [service_providers] service_ ...
- 解决IE11无法下载文件的问题
[问题描写叙述] 单击IE底部下载工具栏没反应,点击"另存为"也没反应 [解决方法] 打开IE11,依次打开菜单:Internet 选项 -> 高级 -> 重置,重置完 ...
- Codeforces 452A Eevee
#include<bits/stdc++.h> using namespace std; string m[]={"vaporeon","jolteon&qu ...
- poj1947(树形dp)
题目链接:http://poj.org/problem?id=1947 题意:给n(n<=150)个点的一棵树,求删掉最少边数k使得最后该树只剩下p(1<=p<=n)个节点.(求最小 ...
- Jersey的RESTful简单案例demo
REST基础概念: 在REST中的一切都被认为是一种资源. 每个资源由URI标识. 使用统一的接口.处理资源使用POST,GET,PUT,DELETE操作类似创建,读取,更新和删除(CRUD)操作. ...
- ubuntu 12.04英文版设置成中文版
适用于ubuntu 12.04英文版的系统,其他版本号的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support ...