zoukankan      html  css  js  c++  java
  • HDU 1506(单调栈)

                Largest Rectangle in a Histogram

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 28460    Accepted Submission(s): 9279


    Problem 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
     
    Source


     题意:如图,就是找最大的面积。
    思路:对于每一个小格子,找到它左面和右面比它大的最远距离。(单调栈口胡)
    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <map>
    #include <iomanip>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <set>
    #include <vector>
    //const int maxn = 1e5+5;
    #define ll long long
    #define inf  0x3f3f3f3f
    #define FOR(i,a,b) for( int i = a;i <= b;++i)
    #define bug cout<<"--------------"<<endl
    
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    const int maxn = 100100;
    using namespace std;
    ll a[maxn];
    int n;
    int l[maxn],r[maxn];
    int main()
    {
    
        //freopen("C:\ACM\input.txt","r",stdin);
        while(scanf("%d",&n) != EOF)
        {
            if(n == 0) break;
            ll maxx = 0;
            for(int i = 1;i <= n; ++i) scanf("%I64d",&a[i]);
    
            stack<int>sta;
            for(int i = 1;i <= n; ++i)
            {
                while(sta.size() && a[i] <= a[sta.top()]) sta.pop();
                if(sta.size()) l[i] = sta.top() + 1;
                else l[i] = 1;
                sta.push(i); 
            }
            while(sta.size()) sta.pop();
            for(int i = n;i >= 1; --i)
            {
                while(sta.size() && a[i] <= a[sta.top()]) sta.pop();
                if(sta.size()) r[i] = sta.top() - 1;
                else r[i] = n;
                sta.push(i);
            }
            for(int i = 1;i <= n; ++i)
            {
                //cout<<l[i]<<" "<<r[i]<<endl;
                ll temp = a[i] * (r[i] - l[i] + 1);
                maxx = max(temp ,maxx);
            }
            printf("%I64d
    ",maxx);
        }
    }
  • 相关阅读:
    如何利用FineBI做财务分析
    mysqlbinlog 读取多个文件
    Chapter 13. Miscellaneous PerlTk Methods PerlTk 方法杂项:
    跨越多台haproxy 跳转
    haproxy redirect prefix
    大数据决策领跑零售业
    大数据决策领跑零售业
    perl 实现微信简版
    perl 调用按钮输出到文本框
    Chapter 11. Frame, MainWindow, and Toplevel Widgets 框架,主窗体,顶级部件
  • 原文地址:https://www.cnblogs.com/jrfr/p/11673181.html
Copyright © 2011-2022 走看看