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 3
4 1000 1000 1000 1000
0
 

Sample Output

8
4000

看别人的题解才知道大概的思路。还是要多做题,多想题~~

题目意思很简单,就算看不懂题目,看图+样例也可以了解。

大意就是:给你一串数字,代表着几个互相连接的矩形,每个数字代表着一个矩形的高,让你求可以得到的最大的矩形面积。(结合题目的图更易看懂~)

看上去我们似乎又要求长,又要求高,似乎很难~~(当时我就是这么想的~~)

但其实,我们可以其中的一个点为中心,然后找左边不大于中心点的矩形的个数,再找右边不大于中心点的矩形的个数,两边个数相加就是矩形的长,而这个中心点的高度就是我们求的矩形的高度~~具体代码如下:

//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的更多相关文章

  1. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  2. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  3. HDU 1506 Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

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

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

  7. hdu 1506 Largest Rectangle in a Histogram(单调栈)

                                                                                                       L ...

  8. HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)

    单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...

  9. HDU 1506 Largest Rectangle in a Histogram【DP】

    题意:坐标轴上有连续的n个底均为1,高为h[i]的矩形,求能够构成的最大矩形的面积. 学习的别人的代码 @_@ 看底的坐标怎么找的看了好一会儿--- 记l[i]为矩形的底的左边的坐标,就将它一直向左扩 ...

随机推荐

  1. Chrome 中的 JavaScript 断点设置和调试技巧

    Console:此功能是模拟js控制台,直接写代码,查看结果.高级功能使用时开启断点,查看变量的变化过程.还可以条用函数. Resources:次功能是查看加载页面所用的资源,链接的数据库,域名下保存 ...

  2. 解决界面有搜索栏时,点击TableView的空白界面,键盘不消失的问题

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(h ...

  3. C++学习笔记 知识集锦(二)

    1. 命名规范 2. 代码格式 3. QString的判断 4. 对象的判空 5. 隐式接口&显式接口 6. vector&string 7. static 8. const 9. v ...

  4. TT3

    crm_kfs_fy     房源 dk_sq_cs       申请测算 dk_zh            贷款账户 dk_ht            借款合同 SS_DICT_MX 数据字典明细 ...

  5. centos6.4 挂载新盘并移动mysql数据目录

    1 centos 挂载新盘并格式化 以vmware workstation12 中安装的centos6. 虚拟机为例进行演示 关机,添加硬盘 开机,格式化硬盘,并挂载 通过命令查看硬盘 fdisk – ...

  6. 拥抱基于jquery.deferred的ajax,和层层嵌套回调的ajax说拜拜

    前言 在项目中,无论是维护自己的代码,还是重构他人的项目代码,看到层层嵌套,异常冗余的某个function,那时我们的内心肯定是奔溃的! 在jquery1.5版本之前,如果我们某个操作需要多个ajax ...

  7. 利用php比较精确的统计在线人数的办法

    利用php比较精确的统计在线人数的办法,注意这里所说的精确是指个数,如果需要精确在时间上,则需要根据实际情况调整代码中的有效时间.(自己没有写,从别人那拿过来的,先放着然后再研究)<?php// ...

  8. ARP协议学习

    1.地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.所以,ARP就是把IP地址解析为MAC地址. 2.如何查看和清除 ...

  9. git基本配置

    用户信息 你个人的用户名称和电子邮件地址,用户名可随意修改,git 用于记录是谁提交了更新,以及更新人的联系方式. $ git config --global user.name "Donl ...

  10. redis 五种数据类型的使用场景

    String 1.String 常用命令: 除了get.set.incr.decr mget等操作外,Redis还提供了下面一些操作: 获取字符串长度 往字符串append内容 设置和获取字符串的某一 ...