zoukankan      html  css  js  c++  java
  • hdu 1506(好题+DP或者RMQ)

    Largest Rectangle in a Histogram

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


    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
     
    好爽啊,用RMQ+二分解法AC了,向左边找的死循环真的困扰了我好久。。当然第二种DP解法容易写一点,但是跳跃性的思维比较难想到
    解法一:RMQ+二分 时间复杂度:O(n*log(n))
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    const int N = 100005;
    int min_dp[N][30];
    long long a[N];
    int L[N],R[N];
    int MIN(int i,int j)
    {
        if(a[i]>a[j]) return j;
        return i;
    }
    void init_RMQ(int n)
    {
        for(int i=1; i<=n; i++)
        {
            min_dp[i][0]=i;
        }
        for(int j=1; (1<<j)<=n; j++)
        {
            for(int i=1; i+(1<<j)-1<=n; i++)
            {
                min_dp[i][j] = MIN(min_dp[i][j-1],min_dp[i+(1<<(j-1))][j-1]);
            }
        }
    }
    
    int MIN_RMQ(int l,int r)
    {
        int k=0;
        while((1<<(k+1))<=(r-l+1)) k++;
        return MIN(min_dp[l][k],min_dp[r-(1<<k)+1][k]);
    }
    int binary(int value,int l,int r) ///找到最右边
    {
        while(l<=r)
        {
            if(l==r) return l;
            int mid = (l+r)>>1;
            if(value<=a[MIN_RMQ(l,mid)])
            {
                l = mid+1;
            }
            else r = mid;
        }
    
    }
    int binary2(int value,int l,int r) ///找到最左边
    {
        while(l<r)
        {
            if(l==r-1){   ///防止死循环,这里弄了好久
                if(a[r]<value) return r;  ///如果在r 并且a[r]<value 那么肯定r就是左边界
           return l; } int mid = (l+r)>>1; if(value<=a[MIN_RMQ(mid,r)]) { r = mid-1; } else l = mid; } return l; } int main() { int n; while(scanf("%d",&n)!=EOF&&n) { for(int i=1; i<=n; i++) { scanf("%lld",&a[i]); } init_RMQ(n); L[1]=1; R[n]=n; for(int i=1; i<=n; i++) { if(i!=n) { R[i] = binary(a[i],i+1,n); if(R[i]==n&&a[i]<=a[n]); else R[i]--; } if(i!=1) { L[i] = binary2(a[i],1,i-1); if(L[i]==1&&a[i]<=a[1]); else L[i]++; } } long long mx = -1; for(int i=1; i<=n; i++) { if((R[i]-L[i]+1)*a[i]>mx) mx = (R[i]-L[i]+1)*a[i]; } printf("%lld ",mx); } }

    解法二:DP 时间复杂度O(n*k) k是个很小的数

    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    const int N = 100005;
    long long a[N],L[N],R[N]; ///L[i]记录i点能够到达最左边的位置,R[i]同理
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF&&n){
            for(int i=1;i<=n;i++){
                scanf("%lld",&a[i]);
            }
            L[1]=1;
            R[n]=n;
            for(int i=2;i<=n;i++){ ///先求出每个坐标最左边能到达的位置
                int t = i;
                while(t>1&&a[i]<=a[t-1]){
                    t = L[t-1];       ///这步相当巧妙,直接实现了跳转,如果a[t-1]不小于a[i]
                                      ///的话,我们可以断定a[t-1]的最左边肯定包含a[i]的最左边.直接跳过中间的点
                                      ///时间复杂度就肯定没有O(n*n)了,应该是O(n*k) k是个比较小的数
                }
                L[i] = t;
            }
            for(int i=n-1;i>=1;i--){ ///找最右边
                int t =i;
                while(t<n&&a[i]<=a[t+1]){
                    t = R[t+1];
                }
                R[i] = t;
            }
            long long mx = -1;
            for(int i=1;i<=n;i++){
                if((R[i]-L[i]+1)*a[i]>mx) mx = (R[i]-L[i]+1)*a[i];
            }
            printf("%lld
    ",mx);
        }
        return 0;
    }
     
  • 相关阅读:
    N4-某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
    N3-按链表值从尾到头的顺序返回一个ArrayList。
    N2-替换字符串空格
    innodb死锁1213
    innodb是行锁还是表锁
    docker是什么,docker干什么用
    innodb通过frm ibd还原数据,线上项目验证过
    机器性能预警系统(cpu,process,内存,硬盘监控)并钉钉机器人报警
    mysql++ query.more_results()为true时,query.store_next()就崩掉了
    yum install nload失败,提示No package nload available.Error: Nothing to do
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5383514.html
Copyright © 2011-2022 走看看