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 ...
随机推荐
- fastica matlab 转载
FastICA工具箱1 http://chunqiu.blog.ustc.edu.cn/?p=68#comment-3512 FastICA代码网址如下:http://research.ics.aal ...
- class ResultServletContextListener implements ServletContextListener
package com.management.spring; import java.io.BufferedReader; import java.io.IOException; import jav ...
- centos yum安装mysql
查看有没有安装包 # yum list mysql 安装mysql客户端 # yum install mysql # yum list mysql-server 安装mysql 服务器端 # yum ...
- iOS开发——屏幕适配篇&Masonry详解
Masonry详解 前言 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-ip ...
- (inline)内联函数在IOS开发中的使用
今天在阅读YYKit源码(https://github.com/ibireme/YYKit.git)时发现在YYKitMacro.h组件中大量使用的内联函数,例如此文件中的一个函数 static in ...
- Kali安全测试相关书籍
Kali安全测试相关书籍http://www.automationqa.com/forum.php?mod=viewthread&tid=4052&fromuid=2
- mysql数据库常用语句3
一:查询指定数据库中所有的表名 数据库名:test select table_name from information_schema.tables where table_schema='test' ...
- 关于php正则表达式模式修饰符
一直以来,以为自己的正则表达式学的还算可以(基本工作上遇到的问题都能解决),但有时候却是知其然而不知其所以然,在网上看到这篇文章,解释了之前的很多疑问,记录下来,随时温故而知新. i (PCRE_CA ...
- [MSDN] 使用 SharePoint 2013 中的 JavaScript 库代码完成基本操作
MSDN:http://msdn.microsoft.com/zh-cn/library/jj163201.aspx 了解如何编写代码以在 SharePoint 2013 中使用 JavaScript ...
- js内置对象处理-打印学生成绩单
效果图: 任务: 1.通过js的内置对象得到当前日期 var date=new Date(); var year=date.toString().slice(11,15); document.writ ...