单调栈POJ-2559

单调栈POJ-2559

题目链接

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
#include<stack>
#include<cstdio>
using namespace std;
typedef long long ll;
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define gc getchar()
template<class T>
inline void read(T&x) {
    x=0;
    char ch=gc;
    bool flag=0;
    while(ch<'0'||ch>'9') {flag|=(ch=='-'),ch=gc;}
    while(ch>='0'&&ch<='9') {x=x*10+ch-48,ch=gc;}
    x=flag?-x:x;
}
ll ans,rlen;//rlen,右边的比当前高的矩形的总宽度
struct N {
    ll w,h;
} x;
stack<N>s;
int main() {
    //freopen("in.txt","r",stdin);
    int n;
    while(1) {
        read(n);
        if(!n)
            break;
        ans=rlen=0;//初始化
        rep(i,0,n-1) {
            read(x.h),x.w=1;//width 默认为1
            if(!s.empty()&&x.h<(s.top()).h) {
                rlen=0;
                while(!s.empty()&&x.h<(s.top()).h) {
                    rlen+=s.top().w;//以当前矩形为标准,向右扩展,左边更矮,扩展不了。
                    ans=max(s.top().h*rlen,ans);//以当前矩形的高作为高,更新ans
                    s.pop();
                }
                x.w+=rlen;//相当于比x.h高的都截成和x.h一样
            }
            s.push(x);
        }
        x.h=x.w=0;
        while(!s.empty()) {
            x.w+=s.top().w;
            x.h=s.top().h;
            ans=max(x.h*x.w,ans);
            s.pop();
        }
        printf("%lld\n",ans);
    }
    return 0;
}


小A的柱状图

链接

题目描述

柱状图是有一些宽度相等的矩形下端对齐以后横向排列的图形,但是小A的柱状图却不是一个规范的柱状图,
它的每个矩形下端的宽度可以是不相同的一些整数,分别为a[i]a[i],每个矩形的高度是h[i]h[i],现在小A只想知道
在这个图形里面包含的最大矩形面积是多少。

输入描述:

一行一个整数N,表示长方形的个数
接下来一行N个整数表示每个长方形的宽度
接下来一行N个整数表示每个长方形的高度

输出描述:

一行一个整数,表示最大的矩形面积

输入

7
1 1 1 1 1 1 1
2 1 4 5 1 3 3

输出

8

备注:

1≤n≤1e6,1≤a[i]≤100,1≤h[i]≤1e9

直接改几个数字就行了

#include<stack>
#include<cstdio>
using namespace std;
typedef long long ll;
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define gc getchar()
template<class T>
inline void read(T&x) {
    x=0;
    char ch=gc;
    bool flag=0;
    while(ch<'0'||ch>'9') {
        flag|=(ch=='-'),ch=gc;
    }
    while(ch>='0'&&ch<='9') {
        x=x*10+ch-48,ch=gc;
    }
    x=flag?-x:x;
}
ll ans,rlen;
const int maxn=1e6+5;
ll W[maxn];
struct N {
    ll w,h;
} x;
stack<N>s;
int main() {
    freopen("in.txt","r",stdin);
    int n;
    read(n);
    ans=rlen=0;
    rep(i,0,n-1)
        read(W[i]);
    rep(i,0,n-1) {
        read(x.h),x.w=W[i];
        if(!s.empty()&&x.h<(s.top()).h) {
            rlen=0;
            while(!s.empty()&&x.h<(s.top()).h) {
                rlen+=s.top().w;
                ans=max(s.top().h*rlen,ans);
                s.pop();
            }
            x.w+=rlen;
        }
        s.push(x);
    }
    x.h=x.w=0;
    while(!s.empty()) {
        x.w+=s.top().w;
        x.h=s.top().h;
        ans=max(x.h*x.w,ans);
        s.pop();
    }
    printf("%lld\n",ans);
    return 0;
}
全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
点赞 评论 收藏
分享
苗条的伊泽瑞尔最喜欢...:同28届被压力了,电科✌就不能去卷算法吗?把Java留给我们双非卷
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务