DP专题训练之HDU 1506 Largest Rectangle in a Histogram
Description

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
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
rectangle in the specified histogram. Remember that this rectangle must
be aligned at the common base line.
Sample Input
Sample Output
看别人的题解才知道大概的思路。还是要多做题,多想题~~
题目意思很简单,就算看不懂题目,看图+样例也可以了解。
大意就是:给你一串数字,代表着几个互相连接的矩形,每个数字代表着一个矩形的高,让你求可以得到的最大的矩形面积。(结合题目的图更易看懂~)
看上去我们似乎又要求长,又要求高,似乎很难~~(当时我就是这么想的~~)
但其实,我们可以其中的一个点为中心,然后找左边不大于中心点的矩形的个数,再找右边不大于中心点的矩形的个数,两边个数相加就是矩形的长,而这个中心点的高度就是我们求的矩形的高度~~具体代码如下:
//Asimple
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
#define INF 0xfffffff
#define mod 1000000
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
#define abs(x) x<0?-x:x
#define srd(a) scanf("%d", &a)
#define src(a) scanf("%c", &a)
#define srs(a) scanf("%s", a)
#define srdd(a,b) scanf("%d %d",&a, &b)
#define srddd(a,b,c) scanf("%d %d %d",&a, &b, &c)
#define prd(a) printf("%d\n", a)
#define prdd(a,b) printf("%d %d\n",a, b)
#define prs(a) printf("%s\n", a)
#define prc(a) printf("%c", a)
using namespace std;
typedef long long ll;
const int maxn = ;
ll n, m, num, T, k;
ll a[maxn], l[maxn], r[maxn]; void input() {
while( ~scanf("%lld", &n) && n ) {
for(int i=; i<=n; i++) {
scanf("%lld", &a[i]);
}
l[] = ;
r[n] = n;
//左边不小于a[i]的数
for(int i=; i<=n; i++) {
k = i;
while( k > && a[i]<=a[k-] ) {
k = l[k-];
}
l[i] = k;
}
//右边不小于a[i]的数
for(int i=n-; i>=; i--) {
k = i;
while( k<n && a[i]<=a[k+]) {
k = r[k+];
}
r[i] = k;
}
ll Max = ;
for(int i=; i<=n; i++) {
if( (r[i]-l[i]+)*a[i] > Max ) {
Max = (r[i]-l[i]+)*a[i];
}
}
printf("%lld\n", Max);
}
} int main(){
input();
return ;
}
DP专题训练之HDU 1506 Largest Rectangle in a Histogram的更多相关文章
- 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: ...
- HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 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题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...
- HDU 1506 Largest Rectangle in a Histogram【DP】
题意:坐标轴上有连续的n个底均为1,高为h[i]的矩形,求能够构成的最大矩形的面积. 学习的别人的代码 @_@ 看底的坐标怎么找的看了好一会儿--- 记l[i]为矩形的底的左边的坐标,就将它一直向左扩 ...
随机推荐
- 数值分析之奇异值分解(SVD)篇
在很多线性代数问题中,如果我们首先思考若做SVD,情况将会怎样,那么问题可能会得到更好的理解[1]. --Lloyd N. ...
- iOS 模拟器变的很多的问题
运行一下命令 xcrun simctl list devices | grep -v '^[-=]' | cut -d "(" -f2 | cut -d ")" ...
- Stripe开发笔记之-ISO Currency Code(ISO 4217)
参考:维基百科和ISO官网 ISO Currency Code:国际标准组织通用货币代号,国际标准化组织(英语:International Organization for Standardizati ...
- 网络监测工具-iftop教程
在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. 一.if ...
- imx6 关闭调试串口
需要关闭imx6调试串口,用作普通的串口使用. 参考链接 http://blog.csdn.net/neiloid/article/details/7585876 http://www.cnblogs ...
- Netty NIO 框架性能压测-短链接-对比Tomcat
压测方案 准备多个文件大小分别为 1k 10k 100k 300k 使用ab分别按 [50,2000](按50逐渐叠加)压测服务,每次请求10W次 硬件信息:CPU:Intel(R) Xeon(R) ...
- 移动BI来袭我们要做哪些准备?
(了解更多商业智能行业资讯.商业智能BI解决方案.商业智能客户案例,请访问:http://www.powerbi.com.cn/service) 随着智能手机的发展,商业智能(BI)基础架构也扩展到移 ...
- !+"\v1" 能判断浏览器类型吗?
我在 http://www.iefans.net/ie-setattribute-bug/ 中看到如此判断是否是IE,if(!+"\v1"){IE代码}else{其他浏览器代码}, ...
- ios通知机制
- js判断手机 横屏模式
js判断手机 横屏模式 方法名称:orientation 实例: if(window.orientation!=0){ var obj=document.getElementById('orienta ...