可惜没如果=_=
时光的河入海流
Largest Rectangle in a Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22171   Accepted: 7173

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

Hint

Huge input, scanf is recommended.

Source

 
此题单调栈即可解决
栈中一个元素的下面一个元素的位置指这个元素可以拓展到的最左端的左边一个,当一个新元素插入需要弹出这个元素时这个新元素的位置为这个元素能拓展到的最右端的右边一个。
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <iostream>
#include "algorithm"
using namespace std;
typedef long long LL;
const int MAX=100005;
LL n;
struct Node{
    LL x;
    LL y;
}sta[MAX];
LL h[MAX],ans;
int main(){
    freopen ("histogram.in","r",stdin);
    freopen ("histogram.out","w",stdout);
    int i,j;
    LL zt;
    while (scanf("%lld",&n),n!=0){
        ans=0;
        for (i=1;i<=n;i++){
            scanf("%lld",h+i);
        }
        int top;
        top=1;
        sta[0].x=0,sta[0].y=0;
        sta[top].x=1;
        sta[top].y=h[1];
        for (i=2;i<=n;i++){
            if (h[i]>=sta[top].y){
                sta[++top].y=h[i];
                sta[top].x=i;
            }
            else{
                while (top>0 && h[i]<sta[top].y){
                    zt=((i-1)-sta[top-1].x)*sta[top].y;
                    ans=max(ans,zt);
                    top--;
                }
                sta[++top].x=i;
                sta[top].y=h[i];
            }
        }
        for (i=1;i<=top;i++){
            ans=max(sta[i].y*(sta[top].x-sta[i-1].x),ans);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
王者重归!

 

posted on 2017-07-23 23:13  珍珠鸟  阅读(170)  评论(0)    收藏  举报