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题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...
随机推荐
- SQL Server 多种分页查询效率
关于SQL语句分页,网上也有很多,我贴一部分过来,并且总结自己已知的分页到下面,方便日后查阅. 方法1 适用于 SQL Server 任何版本 SELECT TOP 页大小 * FROM table1 ...
- js实现导出数据到excel
来自:http://www.imooc.com/article/13374 //html代码<!DOCTYPE HTML> <html> <head> <ti ...
- Flex 最全的换行,制表符,回车,空格......特殊符号
字符 十进制字符编号 实体名字 说明 — &#; — 未使用Unused — &#; — 未使用Unused — &#; — 未使用Unused — &#; — 未使用 ...
- 今日SGU 5.13
SGU 146 题意:就是给你一个长度为l的圈,然后你跑步,每一段给你时间t和速度v,问你最后离起点多远 收获:就是把浮点数转为整数,然后但是会出现精度误差,比如l最多四位小数,那你就加0.00001 ...
- WinSocket 编程
套接字 套接字指通信双方在通信时所使用的通信点(Endpoint),通信的双方通过通信点来交换信息和数据.不同类型的通信会使用不同的类型通信点,比如对于电话通信而言,通信点就是电话号码和分机号码的组合 ...
- spark系统实现yarn资源的自动调度
参考: http://blog.csdn.net/dandykang/article/details/48160953 对于Spark应用来说,资源是影响Spark应用执行效率的一个重要因素. ...
- hdu4565---So Easy!(矩阵)
Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce ...
- Linux中 ps aux 命令
$ ps aux USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND root 11 100.0 0.0 0 16 ?? RL 4Dec09 ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
- 简单说一下 JSON和JSONP
JSON和JSONP,但从缩写看,可能会以为他们是很相似的两个名词,但他们除了缩写相似外,他们是两种类型的概念. 首先: JSON(JavaScript Object Notation)即JavaSc ...