【POJ 2559】 Largest Rectangle in a Histogram

【题目链接】

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

【算法】

          单调栈

【代码】

           

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;
#define MAXN 100010

int i,top,n;
long long a[MAXN],s[MAXN],w[MAXN],stk[MAXN];
long long ans,tmp;

int main() 
{
        
        while (scanf("%d",&n) != EOF && n)
        {
                for (i = 1; i <= n; i++) scanf("%lld",&a[i]);
                ans = -1;
                a[++n] = -1;
                s[top = 0] = -1;
                for (i = 1; i <= n; i++)
                {
                        if (a[i] >= s[top])
                        {
                                s[++top] = a[i];
                                w[top] = 1;
                        } else
                        {
                                tmp = 0;
                                while (a[i] < s[top])
                                {
                                        tmp += w[top];
                                        ans = max(ans,tmp*s[top]);
                                        top--;
                                }
                                s[++top] = a[i]; w[top] = tmp + 1;
                        }
                }
                printf("%lld\n",ans);
        }
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9245487.html
今日推荐