Largest Rectangle in a Histogram

http://acm.hdu.edu.cn/showproblem.php?pid=1506

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem 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
 
Source
 
Recommend
LL   |   
We have carefully selected several similar
problems for you:  1505 1069 1087 1058 1176 
 
题意:有n个数ai。从中选出一段区间[L,R],使得(R-L+1)*min{a_L,…,a_R}最大。
经典广告印刷问题

考虑对于第i个数,求出当这个数成为最小值时,往左往右分别最远能到哪里。

使用单调队列来实现这一过程。

 
#include<cstdio>
#include<algorithm>
#define N 100001 #ifdef WIN32
#define ll "%I64d\n"
#else
#define ll "%lld\n"
#endif using namespace std;
int n,a[N],b[N];
int q[N],tmp[N],head,tail;
int l[N],r[N];
long long ans;
void monotonous(int *c,int *d)
{
q[]=c[]; tmp[]=;
head=; tail=;
for(int i=;i<=n;i++)
{
if(c[i]<q[tail-])
while(head<tail && q[tail-]>c[i]) d[tmp[--tail]]=i-;
q[tail]=c[i];
tmp[tail++]=i;
}
while(head<tail) d[tmp[head++]]=n;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(!n) return ;
ans=;
for(int i=;i<=n;i++) scanf("%d",&a[i]),b[n-i+]=a[i];
monotonous(a,r);
monotonous(b,l);
for(int i=;i<=n;i++) tmp[i]=l[i];
for(int i=;i<=n;i++) l[n-i+]=n-tmp[i]+;
for(int i=;i<=n;i++) ans=max(ans,1ll*(r[i]-l[i]+)*a[i]);
printf(ll,ans);
}
}

uva 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 set+二分

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

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

  4. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

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

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

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

  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. HUD 1506 Largest Rectangle in a Histogram

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

随机推荐

  1. js单行写一个评级组件

    单行写一个评级组件:"★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate); -----------------------------------分隔符- ...

  2. angularJS遇到的坑

    最近在用angularjs做一些东西,由于学艺不精,对angularjs了解不够,导致经常会不小心掉进一些自己挖的坑里(⊙_⊙),在这里记下来,谨防又踩. 1.angularjs ng-show no ...

  3. Android 开发 之 JNI入门 - NDK从入门到精通

    NDK项目源码地址 : -- 第一个JNI示例程序下载 : GitHub - https://github.com/han1202012/NDKHelloworld.git -- Java传递参数给C ...

  4. lintcode-167-链表求和

    167-链表求和 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和. 样例 给出两个链 ...

  5. HDU 2161 Primes

    http://acm.hdu.edu.cn/showproblem.php?pid=2161 Problem Description Write a program to read in a list ...

  6. 字符串数组去重 ["a","b","c","a","b","c"] --> ["a","b","c"]

    非正则实现: let str_arr=["a","b","c","a","b","c&qu ...

  7. 【Linux】- Ubutnu UFW防火墙的简单设置

    ufw是一个主机端的iptables类防火墙配置工具,比较容易上手.一般桌面应用使用ufw已经可以满足要求了. 安装方法 sudo apt-get install ufw 使用方法 1.启用: sud ...

  8. 使用Gulp实现网页自动刷新:gulp-connect

    入门指南 1. 全局安装 gulp: npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装: npm install --save-dev ...

  9. 第71天:jQuery基本选择器(二)

    jQuery选择器 一.内容过滤选择器 选择器 描 述 返 回 示 例 :contains(text) 匹配含有文本内容text的元素 集合元素 $(“p:contains(今天)”) :empty ...

  10. 第64天:CSS常用命名规范,有用!

    CSS常用命名,必须记住 一.常用命名 标题:title 摘要:summary 箭头:arrow 商标:label 网站标志:logo 转角/圆角:corner 横幅广告:banner 子菜单:sub ...