博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2559-Largest Rectangle in a Histogram
阅读量:4557 次
发布时间:2019-06-08

本文共 2638 字,大约阅读时间需要 8 分钟。

Largest Rectangle in a Histogram

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22077   Accepted: 7137

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

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 34 1000 1000 1000 10000

Sample Output

84000

解题心得:

1、其实这个题是动态规划加搜索,先说搜索部分,能够以某个柱形为中心形成公共矩形面积的,只有柱形左方和右方都高于它,所以将每一个柱形为中心向左向右搜索,但是这样毫无疑问会超时。所以需要在这个搜多的基础上优化。

2、然后就是动态规划部分,首先我们可以使用一个right[]组记录一下这个这个柱形最左方的大于它的那个柱形的位置,用left[]数组记录一下最右方的柱形大于它的那位置。当它的下个柱形在搜索的时候,如果比左方的那个柱形高度小就,这个柱形的左右边界就可以直接调到左方的哪个柱形的左右边界再开始找。然后得到答案就行了。

#include
using namespace std;const int maxn = 1e5+10;typedef long long ll;ll num[maxn];ll Right[maxn],Left[maxn];int main(){ ll n; while(scanf("%lld",&n) && n) { memset(Right,0,sizeof(Right)); memset(Left,0,sizeof(Left)); Left[1] = 1; Right[n] = n; for(ll i=1;i<=n;i++) scanf("%lld",&num[i]); //找左方的从左方开始才能够动态规划 for(ll i=2;i<=n;i++) { ll now = i; while(now >= 1 && num[i] <= num[now-1]) now = Left[now-1]; Left[i] = now; } //找右方的就从右方开始 for(ll i=n-1;i>=1;i--) { ll now = i; while(now
Max) Max = sum; } printf("%lld\n",Max); }}

转载于:https://www.cnblogs.com/GoldenFingers/p/9107327.html

你可能感兴趣的文章
M51文件注释
查看>>
关于临界资源访问互斥量的死锁问题
查看>>
django-view层
查看>>
异步加载JS的方法。
查看>>
golang-gorm框架支持mysql json类型
查看>>
【tool】白盒测试
查看>>
图论其一:图的存储
查看>>
20180923-WebService
查看>>
z变换
查看>>
Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数
查看>>
Spring基础2
查看>>
【灵异短篇】这个夜晚有点凉
查看>>
一点小问题
查看>>
pytest 10 skip跳过测试用例
查看>>
MVC身份验证及权限管理
查看>>
It was not possible to find any compatible framework version
查看>>
关于8.0.15版本的mysql下载与安装
查看>>
Redis主从复制看这篇就够了
查看>>
洛谷 P1202 [USACO1.1]黑色星期五Friday the Thirteenth 题解
查看>>
(4.20)SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧...
查看>>