HDU2830-----DP最大子矩阵系列

来源:互联网 发布:淘宝diy u盘好 编辑:程序博客网 时间:2024/05/22 10:22

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2830

题目意思:

给你一个n*m的01矩阵

任意两个列是可以交换的

要你求由1的组成的最大矩阵的元素个数

解题思路:

因为列是可以变化的

我们先枚举以第i行为底的最大子矩阵

对于每一个h[j],我们只要知道所有比它大的有多少个就OK

那怎么求呢

用HASH,这简直就是神器!!!

我们先扫一遍每个h[j]有多少个,然后对于以第i行为底的而言,最大的h[j]也就是i

因为我们是要求大于h[j]的,所以从大到小扫,hash[h[j]]+=hash[h[j]+1]

然后再就是求h[j]*hash[h[j]]了

下面上代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1010;int hash[maxn];int data[maxn][maxn];int h[maxn][maxn];int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)                scanf("%1d",&data[i][j]);        memset(h,0,sizeof(h));        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)                if(data[i][j])                    h[i][j] = h[i-1][j]+1;        }        int ans=-1;        for(int i=1;i<=n;i++)        {            memset(hash,0,sizeof(hash));            for(int j=1;j<=m;j++)            {                hash[h[i][j]]++;            }            for(int j=i;j>=0;j--)                hash[j]+=hash[j+1];            for(int j=1;j<=m;j++)            {                int tmp = h[i][j]*hash[h[i][j]];                if(ans<tmp)                    ans=tmp;            }        }        printf("%d\n",ans);    }    return 0;}


原创粉丝点击