POJ 2559 —— 栈

Largest Rectangle in a Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12796   Accepted: 4133

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: 
POJ 2559 —— 栈_第1张图片
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

Ulm Local 2003

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意是给你n个柱形,问你他们中最大的长方形面积是多少。
我们用栈记录L[i] ,R[i] 两个数组分别表示从i向左找第一个比i低的柱形下标 + 1,从i向右找的第一个比i低的柱形下标。
S = h[i] * (R[i] - L[i])
/*
ID: xinming2
PROG: stall4
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
const int mod = (int)1e9 + 7;
typedef long long LL;
const LL MOD = 1000000007LL;
const double PI = acos(-1.0);

typedef pair<int , int> pi;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int L[MAXN] , R[MAXN] , a[MAXN];
int s[MAXN];
int n;
void solve()
{
    int t = 0;
    for(int i = 0 ; i < n ; i++)
    {
        while(t > 0 && a[s[t - 1]] >= a[i])t--;
        L[i] = t == 0 ? 0 : (s[t - 1] + 1);
        s[t++] = i;
    }
    t = 0;
    for(int i = n - 1 ; i >= 0 ; i--)
    {
        while(t > 0 && a[s[t - 1]] >= a[i])t--;
        R[i] = t == 0 ? n : s[t - 1];
        s[t++] = i;
    }
    LL ans = 0;
    for(int i = 0 ; i < n; i++)
    {
        ans = max(ans , (LL)a[i] * (R[i] - L[i]));
    }
    printf("%lld\n" , ans);
}
int main()
{
    while(scanf("%d", &n) , n)
    {
        clr(s , 0);clr(L ,0);clr(R , 0);
        for(int i = 0; i < n ; i++)scanf("%d" , &a[i]);
        solve();
    }
    return 0;
}



你可能感兴趣的:(栈,ACM)