hdu 2830 Matrix Swapping II

来源:互联网 发布:网络语言的利与弊作文 编辑:程序博客网 时间:2024/04/30 10:18

http://acm.hdu.edu.cn/showproblem.php?pid=2830

先求出每一行的高度,然后对于每一行有多少个 存在高度 用个hash存起来

后面枚举的时候 先排序从大到小,然后 二维只枚举到hash存起来的数就行了,

排序是为了方便求对于每一行求矩阵且节省时间,因为0的就跳过了

矩阵求法 ans=max(height[i][j]*j,ans); 因为前面大的高度就能包含后面小的了所以这里乘以j

代码:

#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#include <iostream>#include <algorithm>#include <map>#include <set>#include <queue>using namespace std;char matrix[1005][1005]={0};int num[1005][1005];int hashs[1005];int cmp(const int a,const int b){    return a>b;}int main(){    int n,m;    while(scanf("%d%d",&n,&m)==2)    {        memset(num,0,sizeof(num));        memset(hashs,0,sizeof(hashs));        for(int i=1;i<=n;i++)        scanf("%s",matrix[i]+1);        for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        {            if(matrix[i][j]=='1')            {                if(num[i-1][j])                num[i][j]=num[i-1][j]+1;                else                num[i][j]++;                hashs[i]++;            }            else            num[i][j]=0;        }        int maxn=0;        for(int i=1;i<=n;i++)        {            sort(num[i]+1,num[i]+1+m,cmp);            for(int j=1;j<=hashs[i];j++)            {                maxn=max(num[i][j]*j,maxn);            }        }        printf("%d\n",maxn);    }    return 0;}