POJ 2559 单调栈

题目链接:

http://poj.org/problem?id=2559

Largest Rectangle in a Histogram

Time Limit: 1000MS
Memory Limit: 65536K
#### 问题描述 > 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. #### 输入 > 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. #### 输出 > 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

题意

给你一块墙,由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-----------------------------------------------------------------------
posted @ 2016-09-01 16:50  fenicnn  阅读(219)  评论(0编辑  收藏  举报