zoukankan      html  css  js  c++  java
  • HDU 1506 Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram

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


    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
     

    题意  求条形图中最大矩形的面积  输入给你条的个数  每个条的高度hi   (以下等于也视为高)

    只要知道第i个条左边连续多少个(a)比他高   右边连续多少个(b)比他高  那么以这个条为最大高度的面积就是hi*(a+b+1);

    但是直接枚举每一个的话肯定会超时的   超时代码

    #include<cstdio>
    using namespace std;
    const int N = 100005;
    typedef long long ll;
    ll h[N]; int n,wide[N];
    int main()
    {
        while (scanf ("%d", &n), n)
        {
            for (int i = 1; i <= n; ++i)
                scanf ("%I64d", &h[i]);
            for (int i = 1; i <= n; ++i)
            {
                wide[i] = 1;
                int k = i;
                while (k > 1 && h[--k] >= h[i]) ++wide[i];
                k = i;
                while (k < n && h[++k] >= h[i]) ++wide[i];
            }
            ll ans = 0;
            for (int i = 1; i <= n; ++i)
                if (h[i]*wide[i] > ans) ans = h[i] * wide[i];
            printf ("%I64d
    ", ans);
        }
        return 0;
    }

    可以发现   当第i-1个比第i个高的时候   比第i-1个高的所有也一定比第i个高  

    于是可以用到动态规划的思想

    令left[i]表示包括i在内比i高的连续序列中最左边一个的编号   right[i]为最右边一个的编号

    那么有   当h[left[i]-1]>=h[i]]时   left[i]=left[left[i]-1]  从前往后可以递推出left[i]   

    同理      当h[right[i]+1]>=h[i]]时   right[i]=right[right[i]+1]   从后往前可递推出righ[i]

    最后答案就等于 max((right[i]-left[i]+1)*h[i])

    #include<cstdio>
    using namespace std;
    const int N = 100005;
    typedef long long ll;
    ll h[N];
    int n, left[N], right[N];
    int main()
    {
        while (scanf ("%d", &n), n)
        {
            for (int i = 1; i <= n; ++i)
                scanf ("%I64d", &h[i]), left[i] = right[i] = i;
            h[0] = h[n + 1] = -1;
            for (int i = 1; i <= n; ++i)
                while (h[left[i] - 1] >= h[i])
                    left[i] = left[left[i] - 1];
            for (int i = n; i >= 1; --i)
                while (h[right[i] + 1] >= h[i])
                    right[i] = right[right[i] + 1];
            ll ans = 0;
            for (int i = 1; i <= n; ++i)
                if (h[i] * (right[i] - left[i] + 1) > ans) ans = h[i] * ll (right[i] - left[i] + 1);
            printf ("%I64d
    ", ans);
        }
        return 0;
    }



  • 相关阅读:
    中等疾病活动度的RA患者持续传统治疗的结果:来自ERAN的数据
    中信国健临床通讯2011年1月第1期目录
    影像学是否应该纳入RA缓解标准?传统评分与修订后复合评分和影像学评估的比较
    RA患者妊娠期使用依那西普维持缓解
    TNFα拮抗剂减少脊髓损伤大鼠神经元和少突胶质细胞的凋亡
    新近起病的活动性RA患者中达标治疗与常规治疗的疗效比较:来自GUEPARD试验和ESPOIR队列的数据
    TNF抑制剂相关的肿瘤风险:阿达木单抗、依那西普和英夫利昔单抗随机对照试验的荟萃分析
    依那西普治疗日本RA患者的安全性与疗效的上市后监察
    依那西普与柳氮磺吡啶治疗强直性脊柱炎的临床疗效与安全性比较:一项随机双盲研究(ASCEND试验)
    POJ3450 Corporate Identity KMP+枚举
  • 原文地址:https://www.cnblogs.com/acvay/p/3947310.html
Copyright © 2011-2022 走看看