POJ 2559 / HDU 1506 Largest Rectangle in a Histogram (DP)

Largest Rectangle in a Histogram

http://poj.org/problem?id=2559

Time Limit: 1000MS

Memory Limit: 65536K

Description

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.

Input

The 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 integersh1,...,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.

Output

For 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

思路:

用2个数组r[i],l[i]表示以h[i]为高的最大矩形的左右边界位置。

注意:

要用先前已经算出来的r,l来加快r[i],l[i]的计算(要不然会TLE)。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索rectangle
, aligned
, of
, The
that
rectangle histogram、largest rectangle、histogram、python histogram、excel histogram,以便于您获取更多的相关知识。

时间: 2024-09-11 16:10:32

POJ 2559 / HDU 1506 Largest Rectangle in a Histogram (DP)的相关文章

[LeetCode]*84.Largest Rectangle in Histogram

题目 Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The large

Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].   The larges

poj 3122 hdu 1969 Pie

点击打开链接poj 3122 点击打开链接hdu' 1969 思路:二分 分析: 1 题目说的是有n块圆形的饼,饼的高度都是1但是半径不一定相同.现在有f+1个人(算上自己),想要平均分得一块面积相同的饼,必须是一块不能够是组合起来的,问这f+1个人能够分到的最大的面积是多少 2 很明显的二分ans,然后对n块饼进行判断,利用二分逼近的思想求出ans 3 注意这样一题的精度要求很高,pi = 3.1415926535898,还有eps不能取太小会TLE. 代码: #include<iostrea

hdu 5461 Largest Point (2015 ACM/ICPC Asia Regional Shenyang Online)

点击打开链接 Largest Point Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 474    Accepted Submission(s): 213 Problem Description Given the sequence A with n integers t1,t2,⋯,tn. Given the integral c

算法:hdu 1011 Starship Troopers (树形背包dp)

题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-1条边的树, 洞穴的入口即根节点是1. 每个洞穴有x只bugs,并有价值y的金子,全部消灭完一个洞穴的虫子,就可以获得这个洞穴的y个金子. 现 在要派m个战士去找金子,从入口进入.每次只有消灭完当前洞穴的所有虫子,才可以选择进入下一个洞穴. 一个战士可以消灭20只虫子,如果要杀死x只虫子,那么要x/20向上取整即(x+19)/20个战士. 如果要 获得某个洞穴的金子,必须留下足够杀死所有虫子的战士数量, 即(x+19)/20个战士,然后这

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

算法:HDU 4679 Terrorist’s destroy(树形dp | 多校8)

题意 给一棵树,每条边有个权值,要删掉一条边,删掉以后会变成两颗子树,设两个子树的直径分别 为d1, d2,删掉的这条边权值为w 问删掉哪一条边,使得w*max(d1, d2)的值最小? 思路 典型的 树形dp, 但比赛时的代码写得非常搓,200+行,还好1A了 f(u, 0):以u点为顶点的子树的直径 f (u, 1):以u的父节点为顶点减去u的子树部分的子树的直径 先用树形dp求出上面的数组 然后 答案等于 min{ w(u,v)*max{f(v,0), f(v,1)} | v为u的子节点

算法:hdu 4003 Find Metal Mineral (树形背包dp)

题意 给一棵n个节点的树, 节点编号为1~n, 每条边都有一个花费值. 有k个机器人从S点出发, 问让 机器人遍历所有边,最少花费值多少? 思路 很好的一题, 推荐! 前天看的这题, 今天才想出来的. 方法想出来后,代码很简单 最近做的几道dp,都是一开始没什么想法,然后过两天再想就想出来了,也许是因 为人的潜意识其实会一直在想某个问题 翻看一下网上其他人的做法, 和我的稍有不同, 他们是用f(i, j)表示子树i用j个机器人的最少花费, 一开始我也是这样 去想,但是没想到怎么去状态转移. 然后

SEERC 2004 / UVa 1330 City Game (扫描)

1330 - City Game Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 Bob is a strategy game programming specialist. In his new city building game the ga