zoukankan      html  css  js  c++  java
  • hdu1506(dp求最大子矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1506

    分析:

    对于每个单位矩阵,我们先求出连续比它高的最左边的下标假设为l,然后求出比它高的最右边的下标假设为r,然后矩阵的面积就是(r-l+1)*1;

    我们从左到右扫一遍,求出每个点的l保存在l[]数组里,然后从右到左扫一遍,求出每个点的r保存在r[]数组里,最后可以求出最大的矩阵了。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define inf 1<<30
    using namespace std;
    LL a[100010],dp[100010],l[100010],r[100010];
    int main()
    {
        int n,t;
        while(scanf("%d",&n)&&n)
        {
            for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
            l[1]=1;r[n]=n;
            for(int i=2;i<=n;i++)//求每个点左边连续比它大的最左边的下标,保存在l[]数组里
            {
                t=i;
                while(t>1&&a[i]<=a[t-1])t=l[t-1];
                l[i]=t;
            }
            for(int i=n-1;i>=1;i--)//求每个点右边连续比它大的最右边的下标,保存在r[]数组里
            {
                t=i;
                while(t<n&&a[i]<=a[t+1])t=r[t+1];
                r[i]=t;
            }
            LL ans=-1;
            for(int i=1;i<=n;i++)
            {
                ans=max(ans,(r[i]-l[i]+1)*a[i]);
            }
            printf("%I64d
    ",ans);
        }
    }
    View Code
  • 相关阅读:
    Nginx中工作进程(work-process)为多少合适?
    Ubuntu中安装启动Nginx
    怎么获得类加载器?
    XML解析方式有哪些?
    HashMap常见面试题
    IO流分类
    集合之间的区别
    css布局2
    css布局1
    css3 总结01
  • 原文地址:https://www.cnblogs.com/lienus/p/4119701.html
Copyright © 2011-2022 走看看