HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11137 Accepted Submission(s): 3047
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.
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
题意 求条形图中最大矩形的面积 输入给你条的个数 每一个条的高度hi (下面等于也视为高)
仅仅要知道第i个条左边连续多少个(a)比他高 右边连续多少个(b)比他高 那么以这个条为最大高度的面积就是hi*(a+b+1);
可是直接枚举每个的话肯定会超时的 超时代码
#include<cstdio>
using namespace std;
const int N = 100005;
typedef long long ll;
ll h[N]; int n,wide[N];
int main()
{
while (scanf ("%d", &n), n)
{
for (int i = 1; i <= n; ++i)
scanf ("%I64d", &h[i]);
for (int i = 1; i <= n; ++i)
{
wide[i] = 1;
int k = i;
while (k > 1 && h[--k] >= h[i]) ++wide[i];
k = i;
while (k < n && h[++k] >= h[i]) ++wide[i];
}
ll ans = 0;
for (int i = 1; i <= n; ++i)
if (h[i]*wide[i] > ans) ans = h[i] * wide[i];
printf ("%I64d\n", ans);
}
return 0;
}
能够发现 当第i-1个比第i个高的时候 比第i-1个高的全部也一定比第i个高
于是能够用到动态规划的思想
令left[i]表示包含i在内比i高的连续序列中最左边一个的编号
right[i]为最右边一个的编号
那么有 当h[left[i]-1]>=h[i]]时 left[i]=left[left[i]-1] 从前往后能够递推出left[i]
同理 当h[right[i]+1]>=h[i]]时 right[i]=right[right[i]+1] 从后往前可递推出righ[i]
最后答案就等于 max((right[i]-left[i]+1)*h[i])了
#include<cstdio>
using namespace std;
const int N = 100005;
typedef long long ll;
ll h[N];
int n, left[N], right[N];
int main()
{
while (scanf ("%d", &n), n)
{
for (int i = 1; i <= n; ++i)
scanf ("%I64d", &h[i]), left[i] = right[i] = i;
h[0] = h[n + 1] = -1;
for (int i = 1; i <= n; ++i)
while (h[left[i] - 1] >= h[i])
left[i] = left[left[i] - 1];
for (int i = n; i >= 1; --i)
while (h[right[i] + 1] >= h[i])
right[i] = right[right[i] + 1];
ll ans = 0;
for (int i = 1; i <= n; ++i)
if (h[i] * (right[i] - left[i] + 1) > ans) ans = h[i] * ll (right[i] - left[i] + 1);
printf ("%I64d\n", ans);
}
return 0;
}
HDU 1506 Largest Rectangle in a Histogram(DP)的更多相关文章
- hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))
# include <stdio.h> # include <algorithm> # include <iostream> # include <math. ...
- 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(区间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 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 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 ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)
单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...
随机推荐
- orm 通用方法——DeleteModel 主键删除
定义代码: /** * 描述:删除对象 * 作者:Tianqi * 日期:2014-09-17 * param:model 对象实例,包含主键 * return:int 受影响行数 * return: ...
- BZOJ 2553 AC自动机+矩阵快速幂 (神题)
思路: 我们先对所有读进来的T建一个AC自动机 因为走到一个禁忌串就需要回到根 所以呢 搞出来所有的结束点 或一下 fail指针指向的那个点 然后我们就想转移 a[i][j]表示从i节点转移到j节点的 ...
- C/C++(C++拷贝构造器,赋值运算符重载)
拷贝构造器 由己存在的对象,创建新对象.也就是说新对象,不由构造器来构造,而是由拷贝构造器来完成.拷贝构造器的格式是固定的. class 类名 { 类名(const 类名 & another) ...
- 紫书 例题 9-13 UVa 1220 (最大独立子集)
这里的状态定义的非常的巧妙,d(i, 1)表示以i为根节点且选i的子树的最大独立子集 d(i, 0)表示以i为根节点且不选i的子树的最大独立子集 d(i, 1) = sum{ d(v, 0) | v是 ...
- wangEditor - 轻量级web富文本编辑器(可带图片上传)
业务需求: 通过后台编辑文章和图片,上传到前端界面,展示新闻消息模块.这个时候,需要一款简洁的编辑器,百度编辑器是最常用的一种,但是功能太过于复杂,而wangEditor - 轻量级web富文本编辑器 ...
- Linux下经常使用的C/C++开源Socket库
1. Linux Socket Programming In C++ : http://tldp.org/LDP/LG/issue74/tougher.html 2. ACE: h ...
- u盘安装14.04ubuntu系统
最近在安装ubuntu 14.04系统,总结了下安装的方法,记录如下 1.下载ubuntu 14.04 iso文件,下载地址 http://www.ubuntu.com/download/deskto ...
- 一些优秀的学习网站(Android)
突然发现自己学习没有总结,从今天开始会持续更新此博文,总结自己的学习情况,也便于自己时常查阅.官方文档就列举了,因为那是必读资料. 一.GitHub部分 1.我的github仓库地址 收藏了我常看的开 ...
- HTTP -- 请求/响应 结构
一:一个HTTP请求报文由四个部分组成:请求行.请求头部.空行.请求数据. 1.请求行 1.请求方法:GET POST 2.URL字段 3.HTTP版本字段 2.请求头 1.Accept:浏览器可接受 ...
- grunt yoman bower的理解
grunt : 前端构建工具 1 什么事前端构建工具 目前前端已经开始了工程化 比如 一个项目 里面用到了几十个js文件 几十个css 很多图片资源 我们如果引入 还是按照以前的方式 out 因此这个 ...