HISTOGRA - Largest Rectangle in a Histogram

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn=1e5+7;
 7 int n,fr1,fr2,tl1,tl2,ans;
 8 int h[maxn],l[maxn],r[maxn],q1[maxn],q2[maxn];
 9 void pre(){
10   memset(h,0,sizeof(h));
11   memset(l,0,sizeof(l));
12   memset(r,0,sizeof(r));
13   memset(q1,0,sizeof(q1));
14   memset(q2,0,sizeof(q2));
15   fr1=1;tl1=0;fr2=n;tl2=n+1;
16   ans=0;
17 }
18 int main(){
19   while(cin>>n){
20     pre();
21     for(int i=1;i<=n;i++) cin>>h[i];
22     q1[++tl1]=1;l[1]=1;
23     for(int i=2;i<=n;i++){
24       if(h[i]<h[q1[tl1]]) l[i]=1;
25       else{
26           int tail=tl1;
27         while(h[i]>=h[q1[tail]]&&tail>=fr1) tail--;
28         l[i]=q1[tail]+1;
29       }
30         if(h[i]<h[q1[tl1]]) q1[++tl1]=i;
31     }
32     q2[--tl2]=n;r[n]=n;
33     for(int i=n-1;i>=1;i--){
34       if(h[i]<h[q2[tl2]]) r[i]=n;
35       else{
36         int tail=tl2;
37         while(h[i]>=h[q2[tail]]&&tail<=fr2) tail++;
38         r[i]=q2[tl2]+1;
39       }
40       if(h[i]<h[q2[tl2]]) q2[--tl2]=i;
41     }
42     for(int i=1;i<=n;i++){
43       ans=max(ans,(r[i]-l[i]+1)*h[i]);
44     }
45     cout<<ans<<endl;
46   }
47   return 0;
48 } 

原文地址:https://www.cnblogs.com/lcan/p/9780917.html

时间: 2024-08-30 17:51:32

HISTOGRA - Largest Rectangle in a Histogram的相关文章

解题报告 『HISTOGRA - Largest Rectangle in a Histogram(单调栈)』

原题地址 单调栈板子题,代码很简单. 注意将a[n + 1]赋值为0,防止栈中矩形未弹完. 代码实现如下: #include <bits/stdc++.h> using namespace std; #define LL long long #define rep(i, a, b) for (register int i = a; i <= b; i++) const int maxn = 1e5 + 5; int n; int a[maxn], sta[maxn], wid[maxn]

(单调栈)poj-2559 Largest Rectangle in a Histogram

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

HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)

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

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

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

POJ 2559 Largest Rectangle in a Histogram(单调栈)

[题目链接]:click here~~ [题目大意]: 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 con

hdu 1506 Largest Rectangle in a Histogram 单调队列

Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12554    Accepted Submission(s): 3509 Problem Description A histogram is a polygon composed of a sequence of rec

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al

poj2559 Largest Rectangle in a Histogram

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 rectang

POJ 2559 Largest Rectangle in a Histogram (栈的运用)

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