POJ 2559 Largest Rectangle in a Histogram -- 动态规划
题目地址:http://poj.org/problem?id=2559
Description
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
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
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
如果确定了长方形的左端点L和右端点R,那么最大可能的高度就是min{hi|L <= i < R}。
L[i] = (j <= i并且h[j-1] < h[i]的最大的j)
R[i] = (j > i并且h[j] < h[i]的最小的j)
#include <stdio.h> #define MAX_N 100000 int n;
int h[MAX_N];
int L[MAX_N], R[MAX_N];
int stack[MAX_N]; long long max(long long a, long long b){
return (a > b) ? a : b;
} void solve(){
//计算L
long long ans = 0;
int t = 0;
int i;
for (i = 0; i < n; ++i){
while (t > 0 && h[stack[t-1]] >= h[i]) t--;
L[i] = (t == 0) ? 0 : (stack[t-1] + 1);
stack[t++] = i;
} //计算R
t = 0;
for (i = n - 1; i >= 0; --i){
while (t > 0 && h[stack[t-1]] >= h[i]) t--;
R[i] = (t == 0) ? n : stack[t-1];
stack[t++] = i;
} for (i = 0; i < n; ++i){
ans = max(ans, (long long)h[i] * (R[i] - L[i]));
}
printf("%lld\n", ans);
} int main(void){
int i;
while (scanf("%d", &n) != EOF && n != 0){
for (i = 0; i < n; ++i)
scanf("%d", &h[i]);
solve();
} return 0;
}
参考资料:挑战程序设计竞赛(第2版)
POJ 2559 Largest Rectangle in a Histogram -- 动态规划的更多相关文章
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15831 ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18942 Accepted: 6083 Description A hi ...
- 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
随机推荐
- sql2008_x64 读取excel
sql2008_x64 读取excel 下载64bit 版的AccessDatabaseEngine_x64:http://www.microsoft.com/en-us/download/detai ...
- 最牛X的编码套路
最近,我大量阅读了Steve Yegge的文章.其中有一篇叫"Practicing Programming"(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反 ...
- Cocos2dx 小技巧(十一) 小人虽短,但能够旋转
转眼五一就到了,放假三天应该做些什么呢?窝在家里钻研技术?写博客?no no no no,这样的"伤害"自己的方式实在让我无法忍受.本来和大学那伙人越好了一起去哪里玩玩,喝酒聊天啥 ...
- iOS-推送通知
推送通知可以做3件事:(1)文字信息(2)一种声音 (3)一个徽章的标记号(第几条消息..) 推送通知流程 (app应用程序--->iOS 设备--->APNS(apple服务器)--- ...
- zend studio 安装xdebug
XDebug安装 到http://xdebug.org/download.php选择自己需要的xdebug版本.然后按照下面的配置建立目录.并在php.ini加入这些内容.重启server 注意xde ...
- C#_mvc_ajax_return data
假设cshtml文件中是这样的: <script type="text/javascript"> $(document).ready(function(){ $(&qu ...
- Tips: compilation and creating new projects on Android 4.0
If you have downloaded android 4.0, you should read the article. $ANDROID_ROOT/tools/android.bat is ...
- 观锁与悲观锁(Hibernate)
乐观锁与悲观锁 文章转自网上好像是玉米田的,忘记了 锁( locking ) 业务逻辑的实现过程中,往往需要保证数据访问的排他性.如在金融系统的日终结算 处理中,我们希望针对某个 cut-off 时间 ...
- Android之网络编程利用PHP操作MySql插入数据(四)
因为最近在更新我的项目,就想着把自己在项目中用到的一些的简单的与网络交互的方法总结一下,所以最近Android网络编程方面的博文会比较多一些,我尽量以最简单的方法给大家分享,让大家明白易懂.如果有什么 ...
- 对比AppScan Source和Fortify扫描AltoroJ的结果
对比AppScan Source和Fortify扫描AltoroJ的结果: http://blog.csdn.net/testing_is_believing/article/details/1963 ...