POJ 2559 单调栈
题目链接:
http://poj.org/problem?id=2559
Largest Rectangle in a Histogram
Memory Limit: 65536K
题意
给你一块墙,由n块矩形排成一行,每块的宽度为1,且第i块的高度为ai,求在墙上能够贴的最大海报的面积
题解
对于每块长条,考虑左边高度比它小且最接近它的和右边高度比它小且最接近它的,那么它的贡献就是以它为高,且左右扩展出去的那块矩形面积了,这个用单调栈维护下左右的最小值就可以了。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
int n;
int arr[maxn],l[maxn],r[maxn];
int main() {
while(scf("%d",&n)==1&&n) {
for(int i=1; i<=n; i++) scf("%d",&arr[i]);
stack<pair<int,int> > st;
st.push(mkp(-1,0));
for(int i=1;i<=n;i++){
while(st.top().X>=arr[i]) st.pop();
l[i]=st.top().Y;
st.push(mkp(arr[i],i));
}
while(!st.empty()) st.pop();
st.push(mkp(-1,n+1));
for(int i=n;i>=1;i--){
while(st.top().X>=arr[i]) st.pop();
r[i]=st.top().Y;
st.push(mkp(arr[i],i));
}
LL ans=0;
for(int i=1;i<=n;i++){
int h=arr[i],w=r[i]-l[i]-1;
ans=max(ans,(LL)h*w);
}
prf("%lld\n",ans);
}
return 0;
}
//end-----------------------------------------------------------------------
不用单调栈,直接地推过去也能维护:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
int n;
int arr[maxn],l[maxn],r[maxn];
int dp[maxn];
int main() {
while(scf("%d",&n)==1&&n) {
for(int i=1; i<=n; i++) scf("%d",&arr[i]);
stack<pair<int,int> > st;
arr[0]=-1;
for(int i=1;i<=n;i++){
int p=i-1;
while(arr[p]>=arr[i]) p=l[p];
l[i]=p;
}
arr[n+1]=-1;
for(int i=n;i>=1;i--){
int p=i+1;
while(arr[p]>=arr[i]) p=r[p];
r[i]=p;
}
LL ans=0;
for(int i=1;i<=n;i++){
int h=arr[i],w=r[i]-l[i]-1;
ans=max(ans,(LL)h*w);
}
prf("%lld\n",ans);
}
return 0;
}
//end-----------------------------------------------------------------------