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. UVALive 3668 A Funny Stone Game(博弈)

    Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2,...,  ...

  2. HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)

    Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...

  3. 衡量失业:失业率(Unemployment Rate)

    定义 劳动力 = 就业人数 + 失业人数 失业率 = (失业人数 / 劳动力) * % 劳动力参与率 = (劳动力 / 成年人口) * %

  4. Linearization of the kernel functions in SVM(多项式模拟)

    Description SVM(Support Vector Machine)is an important classification tool, which has a wide range o ...

  5. Alpha发布-----欢迎来怼团队

    欢迎来怼项目小组—Alpha发布展示 一.小组成员 队长:田继平 成员:葛美义,王伟东,姜珊,邵朔,冉华 ,李圆圆 二.文案+美工展示 链接:http://www.cnblogs.com/wwd199 ...

  6. C# .net 调用QQ邮箱

    public static void QQfs() { try { MailMessage mm = new MailMessage(); MailAddress Fromma = new MailA ...

  7. 3ds Max学习日记(八)

      再来更新一波学习进度.   之前玩了一下3dsmax里的灯光,不过由于和教程里的版本不同,教程里的我的没有,我有的教程又没有,所以只能瞎jb玩一玩.   最近又想建个人物模型玩玩,于是上网搜一下有 ...

  8. idea导出jar包

    在File->Project Structure->Artifacts,如图:  然后: 点击Apply,OK. 跳出去就可以看到多了META-INF文件夹: 然后build项目,就可以看 ...

  9. 在Delphi中如何获得SQL中存储过程的返回值?

    示例存储过程:create procedure proc_loginusername varchar(20),password varchar(20)asdeclare @result intsele ...

  10. TScreen 类 - 通过 Screen 更换光标

    //更换窗体或某个控件的光标可以不通过 Screen 对象, 譬如: begin   Self.Cursor := crAppStart;   Panel1.Cursor := crHandPoint ...