E - Largest Rectangle in a Histogram

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description:

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
本题的题意是给出若干个长方形,高度不一定,问你求得时在整个图形的范围内最大的面积是多少·
由于本题的高度很高,所以用int会出问题,
最好改成long long可以过了
下面给出代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
typedef long long int ll;
ll l[maxn],r[maxn],h[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(!n)
break;
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(h,,sizeof(h));
// memset(dp,0,sizeof(dp));
for(int i=;i<=n;i++){
scanf("%lld",&h[i]); }
l[]=;
r[n]=n; for(int i=;i<=n;i++){
int tmp=i;
while(tmp>&&h[i]<=h[tmp-])
tmp=l[tmp-]; l[i]=tmp;
} for(int i=n-;i>=;i--){
int tmp=i;
while(tmp<n&&h[i]<=h[tmp+])
tmp=r[tmp+];
r[i]=tmp;
}
ll ans=-;
for(int i=;i<=n;i++){
ll tmp=(r[i]-l[i]+)*h[i];
ans=max(ans,tmp);
}
printf("%lld\n",ans);
}
return ;
}

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)的更多相关文章

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

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

  2. hdu 1506 Largest Rectangle in a Histogram ((dp求最大子矩阵))

    # include <stdio.h> # include <algorithm> # include <iostream> # include <math. ...

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

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

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

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

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

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

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

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

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

                                                                                                       L ...

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

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

随机推荐

  1. 重构笔记---MEF框架(下)

    概述 上一篇介绍了MEF的一个很简单很基本的应用,实现了MEF框架并展示了MEF框架的一些基本的要求和设置,这些基础知识很重要,接下来我们分析一下如何扩展或增强MEF框架内容. 增强的Contract ...

  2. 第四章:Javascript表达式和运算符

    表达式是javascript中的一个短语,javascript解释器会将其计算出一个结果.程序中常用量是最简单的一类表达式就是变量.变量名也是一种简单的表达式,它的值就是赋值给变量的值.复杂的表达式是 ...

  3. Nginx下用webbench进行压力测试

    在运维工作中,压力测试是一项非常重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验. 但是,在压力测试中存在一个共性,那就是压力测试的 ...

  4. connection.getResponseCode()!=200

    android在网络编程的时候没有执行connection.getResponseCode()!=200 1.用真机测试的时候,电脑必须和手机连同一个局域网. 2.必须开新线程,不放在主线程里面访问. ...

  5. if转换switch的小技巧

    class Program { static void Main(string[] args) { /* 对学员的结业考试成绩评测(用switch) * 成绩>=90……A * 90>成绩 ...

  6. 【bzoj1037】 ZJOI2008—生日聚会Party

    http://www.lydsy.com/JudgeOnline/problem.php?id=1037 (题目链接) 题意 有n个boy和m个girl排成一排,求使得任意一段的boy个数girl个数 ...

  7. BZOJ3229 石子合并

    Description 在一个操场上摆放着一排N堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分. 试设计一个算法,计算出将N堆石 ...

  8. HackerRank and MiniMax

    传送门 Sherlock and MiniMax Authored by darkshadows on May 07 2014 Problem Statement Watson gives Sherl ...

  9. 单机redis多端口实例+keepalived高可用

    一.实验环境说明 192.168.115.21(keepalived+redis) 192.168.115.95(keepalived+redis) VIP:192.168.115.99 二.安装re ...

  10. abstract class和interface 知多少!!!

    1.相同点   A. 两者都是抽象类,都不能实例化.   B. interface实现类及abstrct class的子类都必须要实现已经声明的抽象方法. 2. 不同点   A. interface需 ...